编程 PostgreSQL日常运维命令总结分享

2024-11-18 06:58:22 +0800 CST views 854

PostgreSQL日常运维命令总结分享

本文总结了PostgreSQL的日常运维命令,包括查看版本、数据库列表、字符集、连接数量、当前用户、数据库运行时间、表空间管理、用户和角色管理、数据库管理、表管理、索引管理、数据库启停、登录、函数、扩展、会话管理等。提供了相应的SQL命令示例,帮助用户高效管理PostgreSQL数据库。

1. 查看版本

postgres=# show server_version;
postgres=# select version();
postgres=# SELECT * FROM pg_catalog.pg_settings WHERE name = 'server_version';

2. 查看数据库列表及编码

postgres=# \l

3. 查看字符集

postgres=# \encoding

4. 查看数据库连接数量

postgres=# select datid, datname, pid, usename, state, client_addr, query from pg_stat_activity;
postgres=# SELECT COUNT(*) FROM pg_stat_activity;
postgres=# show max_connections;

5. 查询当前用户

postgres=# select * from current_user;
postgres=# SELECT SESSION_USER;
postgres=# select * from pg_user;

6. 查看数据库运行时间

postgres=# select pg_postmaster_start_time();
postgres=# SELECT age(now(), pg_postmaster_start_time()) AS uptime;

7. 查看当前用户表列表

postgres=# SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';
postgres=# SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public';

8. 表空间管理

postgres=# \db
postgres=# SELECT pg_tablespace.spcname, pg_size_pretty(pg_tablespace_size(pg_tablespace.oid)) AS size FROM pg_tablespace;
postgres=# select pg_size_pretty(pg_tablespace_size('pg_global'));
postgres=# create tablespace test owner test location '/pgdb/data/test';
postgres=# drop tablespace test;

9. 查看所有 schema

postgres=# select * from information_schema.schemata;
postgres=# \dn

10. 查看数据库大小

postgres=# select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS total_size from pg_database;

11. 查看归档日志设置

postgres=# show archive_mode;
postgres=# show archive_command;

12. 查看锁信息

postgres=# SELECT * FROM pg_locks;

13. 查看表结构

postgres=# \d students
postgres=# SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM information_schema.COLUMNS WHERE TABLE_NAME = 'students';

14. 用户管理

postgres=# create user test1 with password '123456';
postgres=# create user superwith password '123456' superuser;
postgres=# create schema test1 authorization test1;
postgres=# grant all on schema test1 to test1;
postgres=# grant usage on schema test1 to test;
postgres=# alter user superwith password '123123';
postgres=# drop user test;

15. 角色管理

postgres=# \du
postgres=# create role role2;
postgres=# drop role role1;

16. 数据库管理

postgres=# CREATE DATABASE test;
postgres=# DROP DATABASE test;
postgres=# \c test;
postgres=# \ds;

17. 表管理

postgres=# ANALYZE test;
postgres=# alter table test add c3 int;
postgres=# alter table test drop c3;
postgres=# alter table test alter column c3 type varchar(10);
postgres=# \x;

18. 索引管理

postgres=# REINDEX TABLE tablename;
postgres=# \di;

19. 数据库启停管理

[postgres@localhost ~]$ pg_ctl stop;
[postgres@localhost ~]$ pg_ctl start;

20. 数据库登录

[postgres@localhost ~]$ psql;
[postgres@localhost ~]$ psql -U postgres -p 5785 -d postgres -h 192.168.59.138;

21. 查看所有函数

postgres=# \df;
postgres=# SELECT proname, proargtypes, prosrc FROM pg_proc;

22. 查看数据库扩展

postgres=# \dx;

23. 切换工作路径

postgres=# \cd /pgdb;

24. 会话管理

postgres=# \conninfo;
postgres=# select * from pg_stat_activity where state != 'idle';

25. 显示 SQL 执行时间

postgres=# \timing;

26. 将查询结果输出到文件

postgres=# \o test.txt select * from demo1; \o;

27. 执行 SQL 脚本

postgres=# \i test.sql;

28. 序列管理

postgres=# CREATE SEQUENCE snc_seq INCREMENT BY 1 START WITH 1 NO MAXVALUE NO CYCLE CACHE 10;

29. 查看长事务

postgres=# SELECT extract(epoch FROM (clock_timestamp() - xact_start)) AS longtrans FROM pg_stat_activity WHERE state != 'idle';

30. 查找锁阻塞

postgres=# SELECT blocked_locks.pid AS blocked_pid, blocked_activity.usename AS blocked_user, blocking_locks.pid AS blocking_pid FROM pg_locks blocked_locks JOIN pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid JOIN pg_locks blocking_locks ON blocking_locks.locktype = blocked_locks.locktype WHERE blocking_locks.pid != blocked_locks.pid;
复制全文 生成海报 数据库 运维 PostgreSQL SQL 管理

推荐文章

CSS Grid 和 Flexbox 的主要区别
2024-11-18 23:09:50 +0800 CST
微信小程序热更新
2024-11-18 15:08:49 +0800 CST
如何开发易支付插件功能
2024-11-19 08:36:25 +0800 CST
维护网站维护费一年多少钱?
2024-11-19 08:05:52 +0800 CST
Graphene:一个无敌的 Python 库!
2024-11-19 04:32:49 +0800 CST
html夫妻约定
2024-11-19 01:24:21 +0800 CST
如何在Rust中使用UUID?
2024-11-19 06:10:59 +0800 CST
Vue3的虚拟DOM是如何提高性能的?
2024-11-18 22:12:20 +0800 CST
Golang - 使用 GoFakeIt 生成 Mock 数据
2024-11-18 15:51:22 +0800 CST
15 个 JavaScript 性能优化技巧
2024-11-19 07:52:10 +0800 CST
go命令行
2024-11-18 18:17:47 +0800 CST
windows安装sphinx3.0.3(中文检索)
2024-11-17 05:23:31 +0800 CST
Go 并发利器 WaitGroup
2024-11-19 02:51:18 +0800 CST
PHP 代码功能与使用说明
2024-11-18 23:08:44 +0800 CST
Go的父子类的简单使用
2024-11-18 14:56:32 +0800 CST
PHP 8.4 中的新数组函数
2024-11-19 08:33:52 +0800 CST
关于 `nohup` 和 `&` 的使用说明
2024-11-19 08:49:44 +0800 CST
使用Vue 3实现无刷新数据加载
2024-11-18 17:48:20 +0800 CST
智能视频墙
2025-02-22 11:21:29 +0800 CST
Golang 中你应该知道的 Range 知识
2024-11-19 04:01:21 +0800 CST
利用Python构建语音助手
2024-11-19 04:24:50 +0800 CST
前端项目中图片的使用规范
2024-11-19 09:30:04 +0800 CST
程序员茄子在线接单