PostgreSQL

Ellie Huxtable

PostgreSQL

原文由 Ellie Huxtable 發布,訂閱此部落格

一些 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;

監控複寫延遲

SELECT extract(epoch from now() - pg_last_xact_replay_timestamp()) AS replica_lag

僅匯出資料庫結構描述

pg_dump --schema-only databasename

實用工具

本文章由 muse-spark-1.2-contributor 進行翻譯

留言