# 创建表 CREATE TABLE test( id int,body varchar(100) ); # 给表和字段添加注释 COMMENT ON TABLE test IS '这是一个表的注释'; # 给表和字段添加注释 COMMENT ON COLUMN test.id IS '这是一个id的注释'; # 给表和字段添加注释
# 向表中插入数据 insert into test(id,body) values(1,'hello,postgresql');
# 查看当前数据库下所有表 \d
# 查看表结构,相当于 desc \d test
PostgreSQL 使用序列来标识字段的自增长,数据类型有 smallserial、serial 和 bigserial。这些属性类似于 MySQL 数据库支持的 AUTO_INCREMENT 属性。
SMALLSERIAL、SERIAL 和 BIGSERIAL 范围:
伪类型
存储大小
范围
SMALLSERIAL
2字节
1 到 32,767
SERIAL
4字节
1 到 2,147,483,647
BIGSERIAL
8字节
1 到 922,337,2036,854,775,807
示例
1 2 3 4 5 6 7 8 9 10 11 12
# 创建表 CREATE TABLE COMPANY( ID SERIAL PRIMARY KEY, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
# 插入数据 INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Paul', 32, 'California', 20000.00); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Allen', 25, 'Texas', 15000.00);
create table myschema.company( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
# 删除schema: drop schema myschema;
# 删除一个模式以及其中包含的所有对象: DROP SCHEMA myschema CASCADE;
# 创建用户并设置密码 CREATE USER 'username' WITH PASSWORD 'password'; CREATE USER 'test' WITH PASSWORD 'test';
# 修改用户密码 ALTER USER 'username' WITH PASSWORD 'password';
#数据库授权,赋予指定账户指定数据库所有权限 GRANT ALL PRIVILEGES ON DATABASE 'dbname' TO 'username'; #将数据库 mydb 权限授权于 test GRANT ALL PRIVILEGES ON DATABASE mydb TO test; #但此时用户还是没有读写权限,需要继续授权表 GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO xxx; #注意,该sql语句必须在所要操作的数据库里执行
#移除指定账户指定数据库所有权限 REVOKE ALL PRIVILEGES ON DATABASE mydb from test
postgres=# CREATE ROLE david; //默认不带LOGIN属性 CREATE ROLE postgres=# CREATE USER sandy; //默认具有LOGIN属性 CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- david | Cannot login | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {}
postgres=# postgres=# SELECT rolname from pg_roles ; rolname ---------- postgres david sandy (3 rows)
postgres=# CREATE ROLE bella CREATEDB ; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- bella | Create DB, Cannot login | {} david | | {} postgres | Superuser, Create role, Create DB, Replication | {} sandy | | {}
postgres=#
创建角色renee 并赋予其创建数据库及带有密码登录的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13
postgres=# CREATE ROLE renee CREATEDB PASSWORD 'abc123' LOGIN; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- bella | Create DB, Cannot login | {} david | | {} postgres | Superuser, Create role, Create DB, Replication | {} renee | Create DB | {} sandy | | {}
postgres=#
测试renee 角色
1 2 3 4 5
postgres@CS-DEV:~> psql -U renee -d postgres psql (9.1.0) Type "help" for help.
postgres=>
给已存在用户赋予各种权限
赋予登录权限
1 2 3 4 5 6 7 8 9 10 11 12 13
postgres=# ALTER ROLE bella WITH LOGIN; ALTER ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- bella | Create DB | {} david | | {} postgres | Superuser, Create role, Create DB, Replication | {} renee | Create DB | {} sandy | | {}
postgres=#
赋予renee 创建角色的权限
1 2 3 4 5 6 7 8 9 10 11 12 13
postgres=# ALTER ROLE renee WITH CREATEROLE; ALTER ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- bella | Create DB | {} david | | {} postgres | Superuser, Create role, Create DB, Replication | {} renee | Create role, Create DB | {} sandy | | {}