PostgreSQL命令行常用命令psql

注意:除sql语句外的命令大小写敏感!!!sql语句要加上分号结束!!!

如果表名时敏感词 如user等,查询表时要在user上添加双引号.
1、以dbuser的身份连接数据库exampledb:
$ su - dbuser
Password: 
Last login: Wed Mar 1 11:52:07 CST 2017 on pts/
[dbuser@master ~]$ psql -d exampledb

2、查看所用的数据库
postgres=# \l

//注意当语法执行错误时会变为postgres-#,之后执行的语句就出出现错误,所以要保证为postgres=#

3、创建用户数据库,如exampledb:
postgres=# CREATE DATABASE exampledb OWNER dbuser;

4、将exampledb数据库的所有权限都赋予dbuser:
postgres=# GRANT ALL PRIVILEGES ON DATABASE exampledb TO dbuser;

5、使用命令 \q 退出psql:
postgres=# \q

6、创建Linux普通用户,与刚才新建的数据库用户同名,如 dbuser:
$ sudo adduser dbuser
$ sudo passwd dbuser

7、切换数据库,相当于mysql的use dbname
\c dbname

8、列举表,相当于mysql的show tables
\dt

9、查看表结构,相当于desc tblname,show columns from tbname
\d tblname

10、查看索引
\di 

11、删除数据库
DROP DATABASE phone;

12.创建表(大小写均可)
create table test(id integer not null primary key);

13.添加表的字段
alter table phone add column phone_number character varying(11) not null;

14.重命名一个表:
alter table [表名A] rename to [表名B]; 

15.删除一个表:
drop table [表名]; 

16.在已有的表里添加字段:
alter table [表名] add column [字段名] [类型]; 

17.删除表中的字段:
alter table [表名] drop column [字段名]; 

18.重命名一个字段:
alter table [表名] rename column [字段名A] to [字段名B]; 

19.给一个字段设置缺省值:
alter table [表名] alter column [字段名] set default [新的默认值];

20.去除缺省值:
alter table [表名] alter column [字段名] drop default; 

21.在表中插入数据:
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......); 
//eg: insert into phone(id, phone_number) values(1, 18819259295);

22.修改表中的某行某列的数据:
update [表名] set [目标字段名]=[目标值] where [该行特征]; 
eg: update phone set phone_number = replace('phone_number','8', '1');

23.删除表中某行数据:
delete from [表名] where [该行特征]; 
delete from [表名];--删空整个表 

25.创建表:
create table ([字段名1] [类型1] <references 关联表名(关联的字段名)>;,[字段名2] [类型2],......<,primary key (字段名m,字段名n,...)>;); 

26.显示 PostgreSQL 的使用和发行条款
\copyright

27.显示或设定用户端字元编码
\encoding [字元编码名称]

28.SQL 命令语法上的说明,用 * 显示全部命令
\h [名称]

29.提示用户设定内部变数
\prompt [文本] 名称

\password [USERNAME] securely change the password for a user

30.可以使用pg_dump和pg_dumpall来完成。比如备份sales数据库:
pg_dump drupal>/opt/Postgresql/backup/1.bak
//eg: pg_dump test  > /usr/local/pgsql/outputdata/db.sql

命令行导出
pg_dump:将一个PostgreSQL数据库转储到一个脚本文件或者其它归档文件中 

pg_dump mydb > db.sql:将mydb数据库转储到一个 SQL 脚本文件

pg_dump mydb -s > db.sql:将mydb数据库转储到一个 SQL 脚本文件(只导出数据库表结构,不带数据)

pg_dump -D -a -t zones_seq - t interway -t table_3 ... > /tmp/zones_seq.sql:导出部分表,支持通配符,见参考文章1

pg_dumpall:将一个PostgreSQL数据库集群转储到一个脚本文件中

示例:test为待导出的数据库名,后面为导出位置及文件名称

pg_dump test  > /usr/local/pgsql/outputdata/db.sql

命令行导入
psql:psql -U gpadmin -d your-db -f your-table.sql
pg_restore: pg_restore -d your-db your-table.tar

示例:postgresql为用户名,test为要导入的数据库名称,后面为先前导出的数据库文件路径

psql -U postgresql -d test -f /usr/local/pgsql/outputdata/db.sql

31.查询数据表内容
select * from phone;

以你的普通Linux用户名,在数据库中创建同名的用户和数据库,如xxf,然后就可以本机直接连接到数据库xxf了。
~> su - postgres
Password: 
Last login: Wed Mar 1 13:19:02 CST 2017 on pts/1
-bash-4.2$ psql
psql (9.2.18)
Type "help" for help.

postgres=# create user xxf with password '******';
CREATE ROLE
postgres=# create database xxf owner xxf;
CREATE DATABASE
postgres=# grant all privileges on database xxf to xxf;
GRANT
postgres=# \q
-bash-4.2$ exit
logout
~> psql
psql (9.2.18)
Type "help" for help.

xxf=>

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值