【Postgres】Postgres常用命令

文章目录

1、导出数据库某张表

pg_dump -t [表名] -f [导出文件名.sql] [数据库]

只导出insert语句:pg_dump --table=[表名] --data-only --column-inserts [数据库] > [导出文件.sql]

2、导入某张表到数据库

psql -d [数据库] -f [导入文件名.sql]

3、查看数据库占用磁盘页数情况

select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like '[数据表]' and a.reltype=b.oid;

--查看数据库表的磁盘文件路径

select pg_relation_filepath('t_company_risk');

4、查看数据库大小

select pg_size_pretty(pg_database_size('[数据库]'));

5、查看数据表大小

select pg_size_pretty(pg_table_size('t_company_risk'));

6、查看索引大小

select pg_size_pretty(pg_relation_size('t_company_risk_pkey'));

select * from pg_indexes where tablename='t_company_risk'; -- 查看表的索引

7、对数据库中表索引按照大小排序

select indexrelname, pg_size_pretty(pg_relation_size(indexrelid)) from pg_stat_user_indexes where schemaname='public' order by pg_relation_size(relid) desc;

8、对数据库中表按照大小排序

select relname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables where schemaname='public' order by pg_relation_size(relid) desc;

9、回收空间(建议先回收指定表)

vacuum full t_company_risk; -- 回收指定表

10、设置主键自增序列最大值(解决insert时主键冲突)

select setval('t_report_component_id_seq',(select max(id) from t_report_component));

11、创建索引

CREATE INDEX ix_t_company_risk_company_id on t_company_risk(company_id);

CREATE UNIQUE INDEX t_company_risk_pkey ON t_company_risk USING btree (insert_key);--主键索引

12、postgresql开启慢查询日志

1)修改配置文件postgres.conf

vim /data10/pgdata/postgresql.conf

2)加载配置

select pg_reload_conf();

确定是否修改

3)查看log

tail -f /data10/pgdata/pg_log/ postgresql-*.log

13、查看当前正在执行的sql任务

select * from pg_stat_activity;

select pid,application_name,client_addr,query_start,waiting,state,query from pg_stat_activity;

14、查询pg当前连接数

select count(1) from pg_stat_activity;

15、查询pg最大连接数

show max_connections;

16、导出导入数据库

su - postgres

#导出数据库

pg_dump [数据库名]> [导出文件名.sql]

#创建数据库

psql

create database [新数据库名];

#导入数据库

\q

psql -d [新数据库名] -f [导出文件名.sql];

17、vacuum和vacuum full区别

1)vacuum full会lock table ,vacuum不会。

2)vacuum只是将deadtuple的行的空间转换为能够使用的状态,额外空间一般不会返回给os(除非文件末尾有大量的连续空闲空间)。vacuum full会将这些空间碎片后面的数据上移,将数据写入一个新的磁盘文件中。

18、只导出某张表的insert语句

su - postgres

pg_dump --table=t_cascade_lower_info --data-only --column-inserts situation > t_cascade_lower_info20200818.sql

19查询阻塞进程SQL语句

复制代码
SELECT blocked_locks.pid     AS blocked_pid,
         blocked_activity.usename  AS blocked_user,
         blocking_locks.pid     AS blocking_pid,
         blocking_activity.usename AS blocking_user,
         blocked_activity.query    AS blocked_statement,
         blocking_activity.query   AS current_statement_in_blocking_process
   FROM  pg_catalog.pg_locks         blocked_locks
    JOIN pg_catalog.pg_stat_activity blocked_activity  ON blocked_activity.pid = blocked_locks.pid
    JOIN pg_catalog.pg_locks         blocking_locks
        ON blocking_locks.locktype = blocked_locks.locktype
        AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
        AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
        AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
        AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
        AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
        AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
        AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
        AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
        AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
        AND blocking_locks.pid != blocked_locks.pid
 
    JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
   WHERE NOT blocked_locks.granted;
相关推荐
oh,huoyuyan24 分钟前
火语言RPA--Sqlite-导入数据表格
数据库·sqlite·rpa
伏游27 分钟前
【BUG】生产环境死锁问题定位排查解决全过程
服务器·数据库·spring boot·后端·postgresql·bug
搬码红绿灯2 小时前
数据库——MySQL数字函数和子查询
数据库·mysql
侧耳倾听1112 小时前
使用内存数据库来为mapper层的接口编写单元测试
数据库·单元测试
ifanatic2 小时前
[每周一更]-(第138期):MySQL 子查询详解:原理、应用及优化方案
数据库·mysql
TDengine (老段)2 小时前
TDengine 中的日志系统
java·大数据·数据库·物联网·时序数据库·tdengine·iotdb
kfepiza3 小时前
Debian编译安装mysql8.0.41源码包 笔记250401
数据库·笔记·mysql·debian·database
tjfsuxyy3 小时前
SqlServer整库迁移至Oracle
数据库·oracle·sqlserver
老王笔记4 小时前
MySQL统计信息
数据库·mysql