【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;
相关推荐
Mephisto.java4 分钟前
【大数据学习 | kafka高级部分】kafka的优化参数整理
大数据·sql·oracle·kafka·json·database
秋意钟24 分钟前
MySQL日期类型选择建议
数据库·mysql
Dxy12393102161 小时前
python下载pdf
数据库·python·pdf
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
BearHan3 小时前
Sqlsugar调用Oracle的存储过程
oracle·存储过程·orm
superman超哥3 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452003 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库
engchina4 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina4 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j
尘浮生4 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea