PG基本操作入门

1.安装介绍

   windos 下安装,双击安装包,例如:postgressql-10.13-1-windows-x64.exe

输入默认管理用户postgres的密码:<自拟>

2.操作数据库

    2.1 管理工具

          在开始菜单--》PostgresSQL 10 --》pgadmin

    2.2 psql 控制台工具

         cd c:\PostgreSQL\10\bin\

         psql -U postgres 

\l             --> 显示数据库
\c <dbname> --> 切换数据库
\dt --> 显示表
\d <tabname> --> 显示表结构
create database <dbname> -->创建数据库
drop database <dbname> -->删除数据库
\c - <user> -- 切换用户
create user <username> with password '<pwd>' --> 创建用户
create role '<username>' createdb psssword '<pwd>' login --->给予用户登录权限
grant all privileges on database '<dbname>' to '<username>' -->赋予权限
alter role '<username>' supperuser --->超级用户权限

 

3.数据类型

  3.1 数值型

smallint   --》 2byte整数
integer --》 4byte
bigint --》 8
decimal --》 变长
numeric --》变长
real --》 4
double --》 8
serial --》 4byte 自增整数
bigserail --》 8byte自增整数

  3.2 文本型

char(n)
varchar(n)
text

  3.3 时间

timestamp
interval
date
time

  3.4 几何图元

point  --》 16B 点
line   --》 32B 直线
lseg   --》 32B 线段
box    --》 32B 长方形 
path   --》 16 + 16n 闭合路径
polygon  --》40 + 16n 多边形
circle   -->24B   圆   

  3.5 ip地址和ipv6地址

cidr   --》12B or 24B, ipv4、ipv6
inet   --》12B or 24B, ipv4、ipv6
macaddr --》6B ,MAC

        3.6 布尔类型

    boolean TURE or FALSE

  3.7 位串类型

 bit 、bit varying(n)

 create table  test (c1 bit, c2 bit varying(5))

 insert into test values (B'10'::bit(3), B'101');

  3.8.数组

    create table test (c1int, c2 integer[]);

    insert into test values (1, '{100,101,102}');

 

4.pg的DDL操作

alter table test add colunm c3 text;

alter table test drop colunm c3 cascade;

alter table test add check (c1 < 100);

 

5.注意 

  一些与ora的差异

5.1 约束写法 

  pg不带()和DM 一样。

5.2 系统默认值

 9223372036854775807

5.3 ||

  无cancat函数,使用||代替。

5.4 无dual等

  

5.5

 

posted @ 2022-11-14 10:10  疾风泣影  阅读(569)  评论(0)    收藏  举报