【Gerrit】【Postgresql】psql数据库基本操作

1.命令行登录数据库:

psql -p 5432 -Upostgres -W

2.列出所有数据库

\l

3.切换数据库

\c dbname

4.列出当前数据库的所有表

\d


5.查看指定表的所有字段

\d  tablename


6.退出数据库

\q

7.数据库备份

pg_dump -h localhost -U postgres -p 5432 reviewdb|gzip > "`date +%Y%m%d%H%M%S`".db.gz
或
pg_dump --host hostname --port port --username username -t tablename -d dbname >/home/jihite/table.sql 

8.数据库恢复

psql -h 10.125.7.68 -p 5432 -d postgres -U postgres -W postgres -f 2.sql

9.命令导入sql数据文件

psql -h localhost  -d databaseName  -U username -f  filename
psql -h localhost -U username -d databaseName  < /data/20210310165126.db

10.创建数据库

postgres=# CREATE DATABASE reviewdb;
CREATE DATABASE
postgres=# \l

11.创建ROLE

postgres=# CREATE USER user WITH PASSWORD 'xxx';
CREATE ROLE
postgres=# \du

12.修改ROLE密码

postgres=# alter user user with password 'xxx';
ALTER ROLE
postgres=# \du

13.基于postgres数据库的查询
查看连接数:

select count(1) from pg_stat_activity;

查看最大连接数值:

show max_connections;

查看保留连接数:

show superuser_reserved_connections;

查看连接的客户端信息:

SELECT * FROM pg_stat_activity;

参考链接:https://www.cnblogs.com/kaituorensheng/p/4667160.html

posted @ 2020-07-10 17:57  爱啦啦  阅读(642)  评论(0编辑  收藏  举报