服务
pg_ctl start -D /usr/local/var/postgres
pg_ctl stop -D /usr/local/var/postgres
ps aux | grep postgres
查看版本
postgres
显示帮助
psql --help
连接
psql -d postgres
psql -d 数据库名
psql -d 数据库名 -U 用户名
\q
数据库
create database 数据库名;
drop database 数据库名;
\l
\c 数据库名
用户
create user 用户名;
grant all privileges on database 数据库名 to 用户名;
\du
grant select, insert, update, delete on 表名 to 用户名;
revoke select, insert, update, delete on 表名 from 用户名;
Schema
create schema <schema_name>;
select current_schema;
\dn
表
\dt
\d 表名
select * from 表名
select * from 表名 order by 列名
alter table 表名 owner to owner名;
表结构修改
alter table 表名 add 列名 数据类型;
alter table 表名 drop 列名;
alter table 表名 rename 列名 to 新列名;
alter table 表名 alter 列名 type 数据类型;
索引
create index 索引名 on 表名(列名);
drop index 索引名;
视图
create view 视图名 as 视图对应的语句;
\dv
select * from 视图名;
drop view 视图名;
读入外部SQL文件
\i 文件名
文章源自懂站帝-http://www.sfdkj.com/13613.html