PostgreSQL
一些 Postgres 代码片段!这只是一个记录我经常忘记的内容的参考页面。
pg_ctl init -D path- 在path初始化新数据库和配置pg_hba.conf- 配置基于主机的认证pg_ident.conf- 将系统用户映射到数据库用户postgresql.conf- 其他所有配置更改
经验总结
- 先创建不带索引的表,再复制数据,最后添加索引,速度更快
- 对于由 SSD 支撑的数据库,使用
random_page_cost=1.1效果要好得多
代码片段
命令
\l列出数据库\c dbname以当前用户身份连接到数据库
将一个表创建为另一个表的副本
create table new_table as table old_table;注意:这将复制所有数据,但不会复制索引或约束
如不需要数据
create table new_table as table old_table with no data;如果想对其进行查询/筛选:
create table new_table as (select * from old_table where some_condition);检查等待中的锁
select relation::regclass, * from pg_locks where not granted;获取数据库大小
SELECT pg_size_pretty(pg_database_size('database name'));获取表大小
SELECT pg_size_pretty(pg_relation_size('records'));监控复制槽
SELECT * FROM pg_replication_slots;监控复制延迟
SELECT extract(epoch from now() - pg_last_xact_replay_timestamp()) AS replica_lag仅转储数据库架构
pg_dump --schema-only databasename实用工具
随机一篇博客