• PostgreSQL常用管理命令


    常用命令

    查看当前数据库实例的版本信息

    select version();
    
    • 1

    查看数据库的启动时间

    select pg_postmaster_start_time();
    
    • 1

    查看最后load配置文件的时间

    select pg_conf_load_time();
    
    • 1

    使用pg_ctl reload后会改变配置的装载时间

    pg_ctl reload
    
    select pg_conf_load_time();
    
    • 1
    • 2
    • 3

    显示当前数据库时区

     show timezone;
    
    • 1

    查看当前实例中有哪些数据库

     psql -l
     \l
    
    • 1
    • 2

    查看当前用户名

    select user;
    select current_user;
    
    • 1
    • 2

    查看session用户

    select session_user;
    
    • 1

    查询当前连接的数据库名称

    select current_catalog, current_database();
    
    • 1

    查询当前session所在客户端的IP地址及端口

    select inet_client_addr(),inet_client_port();
    
    • 1

    查询当前数据库服务器的IP地址及端口

    select inet_client_addr(),inet_client_port();
    
    • 1

    查询当前session的后台服务进程的PID

    select pg_backend_pid();
    
    • 1

    查看当前参数配置情况

    show shared_buffers;
    
    • 1

    修改当前session的参数配置

     set maintenance_work_mem to '128MB';
     SELECT set_config('maintenance_work_mem', '128MB', false);
    
    • 1
    • 2

    查看当前正在写的WAL文件

    select pg_xlogfile_name(pg_current_xlog_location());
    
    • 1

    查看当前WAL文件的buffer中还有多少字节的数据没有写入磁盘中

    select pg_xlog_location_diff(pg_current_xlog_insert_location(), pg_current_xlog_location());
    
    select pg_xlog_location_diff(pg_current_xlog_insert_location(), pg_current_xlog_location());
    
    • 1
    • 2
    • 3

    查看数据库实例是否正在做基础备份

    select pg_is_in_backup(), pg_backup_start_time() ;
    
    • 1

    查看当前数据库实例处于Hot Standby状态还是正常数据库状态

     select pg_is_in_recovery();
    
    • 1

    查看数据库的大小

    select pg_database_size('osdba'), pg_size_pretty(pg_database_size('osdba'));
    
    • 1

    查看表的大小

    # pg_relation_size()仅计算表的大小,不包括索引的大小,而pg_total_relation_size()则会把表上索引的大小也计算进来
    select pg_size_pretty(pg_relation_size('ipdb2')) ;
    select pg_size_pretty(pg_total_relation_size('ipdb2')) ;
    
    • 1
    • 2
    • 3

    查看表上所有索引的大小

    select pg_size_pretty(pg_indexes_size('ipdb2'));
    
    • 1

    查看表空间的大小

    select pg_size_pretty(pg_tablespace_size('pg_global'));
    
    • 1

    查看表对应的数据文件

    select pg_relation_filepath('test01');
    
    • 1

    系统维护常用命令

    修改配置文件“postgresql.conf”后,要想让修改生效,有以下两种方法。

    pg_ctl reload
    select pg_reload_conf();
    
    • 1
    • 2

    切换log日志文件到下一个

    select pg_rotate_logfile();
    
    • 1

    切换WAL日志文件

    select pg_switch_xlog();
    
    • 1

    手动产生一次checkpoint

    checkpoint;
    
    • 1

    取消正在长时间执行的SQL命令的方法有以下两种

    pg_cancel_backend(pid):取消一个正在执行的SQL命令
    pg_terminate_backend(pid):终止一个后台服务进程,同时释放此后台服务进程的资源
    
    • 1
    • 2

    找出长时间运行的SQL

    select pid,usename,query_start, query from pg_stat_activity;
    
    • 1
  • 相关阅读:
    grunt-processhtml的根据环境打包使用方法,干货
    二百零六、Flume——Flume1.9.0单机版部署脚本(附截图)
    Spring Boot: 约定优于配置的软件设计思想
    python 比较时间 删除mysql数据
    hadoop集群启动后datanode没有启动
    算法自学__树链剖分
    ESP8266-Arduino编程实例-SHT20温湿度传感器驱动
    华为云云耀云服务器L实例评测|部署私有网盘 Nextcloud
    宽压12-90V转5V3A降压IC,AH8691芯片
    第十一章、python的异常处理------raise异常处理、assert异常处理、异常类BaseException、关键字as的用法
  • 原文地址:https://blog.csdn.net/ciqingloveless/article/details/127867606