Postgresql 常用命令搜集整理 —— 筑梦之路

搜集整理postgresql常用操作命令

# 启动数据库

pg_ctl -D /pg/data -l /pg/log/pg.log start

# 登陆数据库 

su  postgres

psql

psql -h 172.16.6.137 -p 5678 -U cjc -W cjcdb

## 参数说明:
-h 指定服务器地址
-p 指定服务端口
-U 指定登陆用户
-W 指定用户密码

# 退出登陆

cjcdb=# \q

# 查看帮助

cjcdb=# \h
cjcdb=# \h create table

# 查看\相关命令

cjcdb=# \?

# 查看数据库版本

cjcdb=# select version();
cjcdb=# show server_version;

# 查看所有的数据库

cjcdb=# \l
cjcdb=# \l+
cjcdb=# select oid,datname from pg_database;

# 查看当前登录的数据库

cjcdb=# select current_database();

# 切换数据库

postgres=# \c cjcdb

# 切换用户

cjcdb=# \c - chen

# 查看当前登录的用户

cjcdb=# select user;
cjcdb=# select current_user;

# 查看当前数据库下所有的表

cjcdb=> \d
cjcdb=> select tableowner,schemaname,tablename,tablespace from pg_tables where tableowner='cjc';

# 查看当前数据库下所有索引

cjcdb=# \di

# 查看索引语句

cjcdb=# select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 't1';
cjcdb=# select pg_get_indexdef(上面语句查到的b.indexrelid);

# 查看登录信息

cjcdb=# \conninfo

# 查看当前连接信息

cjcdb=# select * from pg_stat_activity;

# 查询表结构

cjcdb=> \d t1;
cjcdb=> \d+ t1;  
cjcdb=> select table_schema,table_name,column_name,data_type,character_maximum_length from information_schema.columns where table_name='t1';

# 查询视图

cjcdb=# \dv
cjcdb=# select * from pg_views where schemaname = 'public';
cjcdb=# select * from information_schema.views where table_schema = 'public';

# 查询触发器

cjcdb=# select * from information_schema.triggers;

# 查看序列

cjcdb=# select * from information_schema.sequences where sequence_schema = 'public';

# 查看约束

cjcdb=# select * from pg_constraint where contype = 'p'
cjcdb=# select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 't1';

# 查看表所对应的数据文件路径与大小

cjcdb=# SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 't1';

# 查看表大小

cjcdb=# select pg_relation_size('t1');

# 查询索引大小

cjcdb=# select pg_size_pretty(pg_relation_size('i_t1_id'));

# 查询表+索引总大小

cjcdb=# select pg_size_pretty(pg_total_relation_size('t1'));

# 查询表空间大小

cjcdb=# select pg_size_pretty(pg_tablespace_size('cjctbs'));

# 查询角色信息

cjcdb=# select rolname,rolsuper,rolcreatedb from pg_roles;

# 查询用户角色

cjcdb=# \dg
cjcdb=# \du

# 查询用户表权限

cjcdb=# select * from information_schema.table_privileges where grantee='cjc';
cjcdb=# \dp

# 查看表空间

cjcdb=# \db

# 查询数据文件位置

cjcdb=# show data_directory;

# 查询配置文件位置

cjcdb=# show config_file;

# 查询数据库大小

cjcdb=# select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database; 
cjcdb=# select pg_size_pretty(pg_database_size('cjcdb'));
格式化输出

# 按列显示 类似mysql的\G

cjcdb=# \x
Expanded display is on.
cjcdb=# select * from t1;
...     
cjcdb=# \x
Expanded display is off.

# 显示执行时间

cjcdb=# \timing on
cjcdb=# \timing of

# 执行shell命令 类似oracle的ho,MySQL的system命令

cjcdb=# \! pwd

# 格式化输出\pset

cjcdb=# select * from t1;
 id |    name    
----+------------
  1 | a         
  2 | aaa       
(2 rows)

cjcdb=# \pset border 2
Border style is 2.
cjcdb=# select * from t1;
+----+------------+
| id |    name    |
+----+------------+
|  1 | a          |
|  2 | aaa        |
+----+------------+
(2 rows)

# 取消边框

cjcdb=# \pset border 0
Border style is 0.
cjcdb=# select * from t1;
id    name    
-- ----------
 1 a         
 2 aaa       
(2 rows)

# 调整分隔符为"|"

cjcdb=# \pset format unaligned
Output format is unaligned.
cjcdb=# select * from t1;
id|name
1|a         
2|aaa       
(2 rows)

# 调整分隔符为"Tab"

cjcdb=# \pset fieldsep '\t'
Field separator is "  ".
cjcdb=# select * from t1;
id  name
1  a         
2  a

# 输出结果到文本 类似oracle spool命令、MySQL tee命令

cjcdb=# \o t1.txt
cjcdb=# select * from t1;
[postgres@cjc-db-01 ~]$ cat t1.txt 
id  name
1  a         
2  aaa       
(2 rows)

# 显示信息

cjcdb=# \echo hahaha
hahaha

# 执行sql脚本


[postgres@cjc-db-01 ~]$ cat t1.sql 
select * from t1;
[postgres@cjc-db-01 ~]$ psql -h 172.16.6.137 -p 5678 -U cjc -W cjcdb -f t1.sql 
Password for user cjc: 
 id |    name    
----+------------
  1 | a         
  2 | aaa       
(2 rows)

# 获取快捷键实际执行的命令(参数-E)

例如,查询有哪些数据库

快捷键

查看有哪些数据库

cjcdb=# \l

实际执行的SQL

cjcdb=# \l
********* QUERY **********
SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************

猜你喜欢

转载自blog.csdn.net/qq_34777982/article/details/128780781