數據定義
數據庫
-- 建立數據庫
-- https://www.postgresql.org/docs/current/static/multibyte.html
-- database_name,數據庫名稱
-- database_user,用戶名
CREATE DATABASE {database_name} WITH OWNER = {database_user};
CREATE DATABASE {database_name} OWNER {database_user};
-- LC_COLLATE:string sort order
-- LC_CTYPE:character classification
-- database_name,數據庫名稱
-- database_user,用戶名
CREATE DATABASE {database_name} WITH OWNER = {database_user} ENCODING 'UTF8' LC_COLLATE = 'zh_CN.UTF-8' LC_CTYPE = 'zh_CN.UTF-8';
-- OR WINDOWS
CREATE DATABASE {database_name} WITH OWNER = {database_user} ENCODING 'UTF8' LC_COLLATE = 'Chinese (Simplified)_China.936' LC_CTYPE = 'Chinese (Simplified)_China.936';
-- 複製數據庫
-- database_name,數據庫名稱
-- database_user,用戶名
-- original_database_name,原始數據庫名稱
CREATE DATABASE {database_name} WITH TEMPLATE {original_database_name} OWNER {database_user};
表
-- 新增列
-- table_name,表名
-- column_name,列名
-- column_type,列類型
ALTER TABLE {table_name} ADD COLUMN IF NOT EXISTS {column_name} {column_type} [NULL | NOT NULL];
擴展
-- 建立 UUID 擴展
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- 驗證 UUID 擴展
SELECT uuid_generate_v4();
-- 建立 cube 擴展
CREATE EXTENSION IF NOT EXISTS cube;
-- 建立 earthdistance 擴展
CREATE EXTENSION IF NOT EXISTS earthdistance;
函數
-- 隱式將整形轉換成字符串,可是會有一些問題,參考 https://stackoverflow.com/questions/50025750/postgres-convert-integer-into-text。一般狀況下仍是建議使用 CAST 函數來實現。
-- 使用場景:在數據庫遷移的時候(好比 Microsoft SQL Server 轉成 PostgreSQL,Microsoft SQL Server 默認是支持的)須要隱式轉換,以達到快速實現的目的
CREATE FUNCTION pg_catalog.text(integer) RETURNS text STRICT IMMUTABLE LANGUAGE SQL AS 'SELECT textin(int4out($1));';
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
COMMENT ON FUNCTION pg_catalog.text(integer) IS 'convert integer to text';
CREATE FUNCTION pg_catalog.text(bigint) RETURNS text STRICT IMMUTABLE LANGUAGE SQL AS 'SELECT textin(int8out($1));';
CREATE CAST (bigint AS text) WITH FUNCTION pg_catalog.text(bigint) AS IMPLICIT;
COMMENT ON FUNCTION pg_catalog.text(bigint) IS 'convert bigint to text';
索引
-- 建立索引
CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} USING btree ({column_name});
-- Query the indexes of a table
-- table_name,表名
SELECT * FROM pg_indexes WHERE tablename IN ('{table_name}');
-- 查詢全部索引
SELECT
i.relname AS indname ,
i.relowner AS indowner ,
idx.indrelid::REGCLASS ,
am.amname AS indam ,
idx.indkey ,
ARRAY(
SELECT
pg_get_indexdef(idx.indexrelid,
k + 1,
TRUE)
FROM
GENERATE_SUBSCRIPTS(idx.indkey, 1) AS k
ORDER BY
k) AS indkey_names ,
idx.indexprs IS NOT NULL AS indexprs ,
idx.indpred IS NOT NULL AS indpred
FROM
pg_index AS idx
JOIN pg_class AS i ON
i.oid = idx.indexrelid
JOIN pg_am AS am ON
i.relam = am.oid
JOIN pg_namespace AS ns ON
ns.oid = i.relnamespace
AND ns.nspname = ANY (CURRENT_SCHEMAS(FALSE));
-- 查詢全部索引,排除系統表
SELECT
U.usename AS user_name,
ns.nspname AS schema_name,
idx.indrelid :: REGCLASS AS table_name,
i.relname AS index_name,
idx.indisunique AS is_unique,
idx.indisprimary AS is_primary,
am.amname AS index_type,
idx.indkey,
ARRAY(
SELECT
pg_get_indexdef(idx.indexrelid,
k + 1,
TRUE)
FROM
GENERATE_SUBSCRIPTS(idx.indkey, 1) AS k
ORDER BY
k ) AS index_keys,
(idx.indexprs IS NOT NULL)
OR (idx.indkey::INT[] @> ARRAY[0]) AS is_functional,
idx.indpred IS NOT NULL AS is_partial
FROM
pg_index AS idx
JOIN pg_class AS i ON
i.oid = idx.indexrelid
JOIN pg_am AS am ON
i.relam = am.oid
JOIN pg_namespace AS NS ON
i.relnamespace = NS.OID
JOIN pg_user AS U ON
i.relowner = U.usesysid
WHERE
NOT nspname LIKE 'pg%';
數據查詢和操做
-- 檢查不存在則寫入
INSERT INTO {table_name}({column_name1} [, {column_name2}, ...])
SELECT {column_value1} [, {column_value2}, ...]
WHERE NOT EXISTS (
SELECT 1 FROM {table_name} WHERE ...
)
權限控制
-- CREATE USER OR ROLE,PostgreSQL 中建立用戶和角色是等效的
-- role_name,用戶角色名稱
-- user_password,用戶密碼
-- user_name,用戶角色名稱
CREATE ROLE {role_name} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{user_password}';
CREATE user {user_name} PASSWORD '{user_password}';
-- 分配全部權限
-- database_name,數據庫名稱
-- database_user,數據庫用戶
GRANT ALL PRIVILEGES ON {database_name} TO {database_user};
-- 修改表的 Owner
-- table_name,表名
-- database_user,數據庫用戶
ALTER TABLE {table_name} OWNER TO {database_user};
-- 分配 FUNCTION 的權限給指定用戶
-- function_name,函數名稱
-- parameter1_type,第一個函數參數類型
-- parameter2_type,第二個函數參數類型
-- database_user,數據庫用戶
GRANT EXECUTE ON FUNCTION {function_name}([{parameter1_type}, {parameter2_type}, ...]) TO {database_user};
-- 修改 FUNCTION 的 Owner
-- function_name,函數名稱
-- parameter1_type,第一個函數參數類型
-- parameter2_type,第二個函數參數類型
-- database_user,數據庫用戶
ALTER FUNCTION {function_name}([{parameter1_type}, {parameter2_type}, ...]) OWNER TO {database_user};
運行分析
-- 查詢當前數據庫 TOP 20 大表
SELECT table_name
,pg_size_pretty(pg_relation_size(table_schema || '.' || table_name)) AS size
FROM information_schema.tables
ORDER BY pg_relation_size(table_schema || '.' || table_name) DESC LIMIT 20;
-- 查詢單個表大小
-- table_name,表名
SELECT pg_size_pretty(pg_relation_size({table_name}));
-- 查詢數據庫活動的查詢
SELECT current_timestamp - query_start AS runtime
,query_start
,datname
,pid
,query
FROM pg_stat_activity
WHERE query_start IS NOT NULL
ORDER BY 1 DESC limit 20;
運行維護
-- Cancel Processes by pid
SELECT pg_cancel_backend(pid int);
-- Terminate Processes by pid
SELECT pg_terminate_backend(pid int);
-- Kill all existing connections in the original database
-- source_db,數據庫名稱
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '{source_db}'
AND pid <> pg_backend_pid();
-- garbage-collect and optionally analyze a database
-- table_name,數據庫表名
VACUUM {table_name};
VACUUM FULL {table_name};
配置
-- 修改 max_locks_per_transaction
ALTER SYSTEM SET max_locks_per_transaction = 300;
-- 重載配置信息,使配置生效
-- pg_hba.conf
SELECT pg_reload_conf();
備份還原
# host_name,主機
# database_user,數據庫用戶
# file_path,備份文件路徑
# database_name,數據庫名稱
pg_dump --host {host_name} --port {port} --username {database_user} --format c --blobs --verbose --file {file_path} {database_name}
pg_restore --host {host_name} --port {port} --username {database_user} --no-owner --dbname {database_name} {file_path}
其餘
-- Prepare a statement for execution
PREPARE foo(TEXT, TEXT, TEXT) AS
SELECT *
FROM foobar
WHERE foo = $1
AND bar = $2
OR baz = $3
EXECUTE foo('foo', 'bar', 'baz');
DEALLOCATE foo;
時間處理
-- 查詢時間差
-- begin_time
-- end_time
SELECT EXTRACT(epoch FROM ({begin_time} - {end_time}));
-- Query the last month in format 'YYYYMM'
SELECT to_char(date_trunc('month', current_date - interval '1' month), 'YYYYMM');
psql
# 打開數據庫鏈接
# host_name
# database_user
psql -h {host_name} -U {database_user}
# 列出全部的數據庫
\l
# 鏈接數據
# database_name
\c {database_name}
File Locations
SHOW data_directory;
SHOW config_file;
SHOW hba_file;
修改記錄
- 2020-03-23 21:44 新增數據查詢和操做的 SQL
- 2020-03-17 18:29 新增 File Locations 節點
- 2020-03-12 23:02 修改備份腳本:增長了 port 參數;將縮寫命令改爲完整參數命令,便於閱讀