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'));監控 replication slots(複寫插槽)
SELECT * FROM pg_replication_slots;監控 replication lag(複寫延遲)
SELECT extract(epoch from now() - pg_last_xact_replay_timestamp()) AS replica_lag僅傾印資料庫結構描述
pg_dump --schema-only databasename實用工具
隨機一篇部落格