【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;
相关推荐
Cachel wood2 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Python之栈9 分钟前
【无标题】
数据库·python·mysql
风_流沙21 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
亽仒凣凣29 分钟前
Windows安装Redis图文教程
数据库·windows·redis
亦世凡华、38 分钟前
MySQL--》如何在MySQL中打造高效优化索引
数据库·经验分享·mysql·索引·性能分析
YashanDB40 分钟前
【YashanDB知识库】Mybatis-Plus调用YashanDB怎么设置分页
数据库·yashandb·崖山数据库
ProtonBase1 小时前
如何从 0 到 1 ,打造全新一代分布式数据架构
java·网络·数据库·数据仓库·分布式·云原生·架构
云和数据.ChenGuang6 小时前
Django 应用安装脚本 – 如何将应用添加到 INSTALLED_APPS 设置中 原创
数据库·django·sqlite
woshilys7 小时前
sql server 查询对象的修改时间
运维·数据库·sqlserver
Hacker_LaoYi7 小时前
SQL注入的那些面试题总结
数据库·sql