PostgreSQL 速查表
可搜索、可打印的 PostgreSQL 参考——psql 命令、JSONB、数组、CTE、窗口函数、索引、角色和 EXPLAIN。免费。
psql meta-commands
15psql -U user -d mydb -h localhost
Connect to a database
\l
List all databases
\c mydb
Switch to another database
\dt
List tables in current schema
\d users
Describe a table (columns, indexes)
\d+ users
Describe with size and storage
\dn
List schemas
\df
List functions
\du
List roles and their attributes
\x
Toggle expanded (vertical) output
\timing
Toggle query execution timing
\e
Edit the query buffer in $EDITOR
\i script.sql
Run SQL from a file
\copy users TO 'users.csv' CSV HEADER
Export a table to CSV
\q
Quit psql
Querying
10SELECT * FROM users LIMIT 10;
First 10 rows
WHERE name ILIKE '%ann%'
Case-insensitive pattern match
WHERE name ~* '^a.*z$'
Regex match (case-insensitive)
ORDER BY score DESC NULLS LAST
Control where nulls sort
SELECT DISTINCT ON (user_id) * FROM orders ORDER BY user_id, created_at DESC;
Latest row per group
LIMIT 10 OFFSET 20
Paginate results
FETCH FIRST 5 ROWS ONLY
SQL-standard limit
SELECT ... FOR UPDATE SKIP LOCKED
Queue-style row locking
TABLESAMPLE SYSTEM (1)
Sample ~1% of a table
LATERAL (SELECT ... LIMIT 3)
Per-row correlated subquery
JSONB
14data->'user'
Get JSON field (as jsonb)
data->>'name'
Get JSON field as text
data#>>'{user,address,city}'
Get nested value by path as text
data @> '{"active": true}'
Contains — matches subdocument
data ? 'email'
Key exists at top level
data ?| array['a', 'b']
Any of the keys exist
jsonb_set(data, '{name}', '"Sam"')
Update a value by path
data - 'legacy_field'
Delete a key
data || '{"active": true}'
Merge/overwrite fields
jsonb_array_elements(data->'items')
Expand array to rows
jsonb_each(data)
Expand object to key/value rows
jsonb_build_object('id', id, 'name', name)
Build JSON from columns
jsonb_agg(row_to_json(t))
Aggregate rows into a JSON array
jsonb_pretty(data)
Pretty-print for debugging
Arrays & ranges
13tags TEXT[]
Declare an array column
ARRAY['a', 'b', 'c']
Array literal
tags[1]
Element access (1-based!)
WHERE 'php' = ANY(tags)
Array contains a value
WHERE tags @> ARRAY['php','sql']
Array contains all values
WHERE tags && ARRAY['php','go']
Arrays overlap (share any)
array_agg(name ORDER BY name)
Aggregate values into an array
unnest(tags)
Expand array to rows
array_length(tags, 1)
Number of elements
string_to_array('a,b', ',')
Split text into an array
period tstzrange
Timestamp range column
WHERE period @> now()
Range contains a value
EXCLUDE USING gist (room WITH =, period WITH &&)
No overlapping bookings
CTEs & window functions
12WITH recent AS (SELECT ...) SELECT * FROM recent;
Common table expression
WITH RECURSIVE tree AS (... UNION ALL ...)
Walk hierarchies/graphs
WITH del AS (DELETE ... RETURNING *) INSERT ...
Data-modifying CTE
ROW_NUMBER() OVER (ORDER BY score DESC)
Sequential rank, no ties
RANK() / DENSE_RANK() OVER (...)
Rankings with tie handling
OVER (PARTITION BY user_id ORDER BY at)
Window per group
LAG(price) OVER (ORDER BY day)
Previous row's value
LEAD(price, 2) OVER (...)
Value 2 rows ahead
SUM(amount) OVER (ORDER BY day)
Running total
AVG(x) OVER (ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
7-row moving average
NTILE(4) OVER (ORDER BY score)
Split rows into quartiles
FIRST_VALUE(name) OVER (...)
First value in the window
Upsert & RETURNING
9INSERT ... ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
Upsert on a unique column
ON CONFLICT DO NOTHING
Silently skip duplicates
ON CONFLICT ON CONSTRAINT users_pkey DO UPDATE ...
Target a named constraint
EXCLUDED.column
The value that failed to insert
DO UPDATE ... WHERE users.updated_at < EXCLUDED.updated_at
Conditional upsert
INSERT ... RETURNING id;
Get generated ids back
UPDATE ... RETURNING *;
Return the updated rows
DELETE ... RETURNING id, email;
Return what was deleted
MERGE INTO t USING src ON ... (PG 15+)
SQL-standard merge
Data types
13id BIGINT GENERATED ALWAYS AS IDENTITY
Modern auto-increment (prefer)
SERIAL / BIGSERIAL
Legacy auto-increment (sequence)
UUID DEFAULT gen_random_uuid()
Native UUID with generator
TEXT
Unlimited string — no VARCHAR(n) needed
NUMERIC(10, 2)
Exact decimal (money math)
TIMESTAMPTZ
Timestamp with time zone (prefer)
INTERVAL '2 days 3 hours'
Duration type, date arithmetic
BOOLEAN
True/false/null (real type)
JSONB
Binary JSON — indexable, prefer over JSON
BYTEA
Raw binary data
INET / CIDR / MACADDR
Network address types
CREATE TYPE status AS ENUM ('new', 'done');
Custom enum type
col TEXT GENERATED ALWAYS AS (lower(email)) STORED
Generated column
Indexes
12CREATE INDEX idx_users_email ON users (email);
Default B-tree index
CREATE UNIQUE INDEX ... ON users (lower(email));
Unique expression index
CREATE INDEX ... ON orders (user_id) WHERE status = 'open';
Partial index
CREATE INDEX ... USING gin (data);
GIN — jsonb/array containment
CREATE INDEX ... USING gin (to_tsvector('english', body));
Full-text search index
CREATE INDEX ... USING gist (location);
GiST — geometry, ranges, KNN
CREATE INDEX ... USING brin (created_at);
BRIN — huge append-only tables
CREATE INDEX CONCURRENTLY ...
Build without locking writes
CREATE INDEX ... ON t (a, b DESC);
Composite with sort direction
CREATE INDEX ... ON t (a) INCLUDE (b);
Covering index (index-only scan)
REINDEX INDEX CONCURRENTLY idx_name;
Rebuild a bloated index
DROP INDEX CONCURRENTLY idx_name;
Remove without blocking
Roles & privileges
11CREATE ROLE app LOGIN PASSWORD 'secret';
Create a login role (user)
CREATE ROLE readonly NOLOGIN;
Create a group role
GRANT readonly TO app;
Add a role to a group
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
Bulk grant reads
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
Auto-grant future tables
GRANT USAGE ON SCHEMA public TO app;
Allow schema access
REVOKE INSERT ON users FROM app;
Remove a privilege
ALTER ROLE app SET statement_timeout = '30s';
Per-role setting
ALTER TABLE users OWNER TO app;
Change object owner
ALTER TABLE docs ENABLE ROW LEVEL SECURITY;
Turn on row-level security
CREATE POLICY own ON docs USING (owner_id = current_user_id());
RLS policy
Maintenance & EXPLAIN
12EXPLAIN SELECT ...;
Show the planned query plan
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
Run it and show real timings
VACUUM (VERBOSE) users;
Reclaim dead-row space
VACUUM FULL users;
Rewrite table (locks it!)
ANALYZE users;
Refresh planner statistics
SELECT * FROM pg_stat_activity;
List running queries
SELECT pg_cancel_backend(pid);
Cancel a running query
SELECT pg_terminate_backend(pid);
Kill a connection
SELECT pg_size_pretty(pg_total_relation_size('users'));
Table size incl. indexes
SELECT pg_size_pretty(pg_database_size('mydb'));
Whole database size
SELECT * FROM pg_stat_user_indexes;
Spot unused indexes
SELECT * FROM pg_locks WHERE NOT granted;
Find blocked lock waits
Backup & restore
12pg_dump mydb > mydb.sql
Plain-SQL dump of a database
pg_dump -Fc mydb > mydb.dump
Custom format (compressed)
pg_dump -Fd mydb -j 4 -f dumpdir/
Directory format, 4 parallel jobs
pg_dump -t users -t orders mydb
Dump specific tables only
pg_dump --schema-only mydb
Structure without data
pg_dump --data-only mydb
Data without structure
psql mydb < mydb.sql
Restore a plain-SQL dump
pg_restore -d mydb -j 4 mydb.dump
Restore custom format in parallel
pg_restore -l mydb.dump
List dump contents
pg_restore -t users -d mydb mydb.dump
Restore a single table
pg_dumpall --globals-only > roles.sql
Back up roles and tablespaces
pg_basebackup -D /backup -Fp -Xs -P
Physical base backup (PITR)
Extensions
11CREATE EXTENSION IF NOT EXISTS pgcrypto;
Hashing, encryption functions
CREATE EXTENSION "uuid-ossp";
uuid_generate_v4() and friends
CREATE EXTENSION pg_stat_statements;
Track query performance stats
CREATE EXTENSION pg_trgm;
Trigram fuzzy search + LIKE index
CREATE EXTENSION postgis;
Geospatial types and queries
CREATE EXTENSION vector;
pgvector — embeddings & ANN search
CREATE EXTENSION hstore;
Key/value pairs in a column
CREATE EXTENSION postgres_fdw;
Query remote Postgres servers
CREATE EXTENSION citext;
Case-insensitive text type
SELECT * FROM pg_available_extensions;
List installable extensions
\dx
List installed extensions (psql)
没有条目匹配“:q”。
关于 PostgreSQL 速查表
这份 PostgreSQL 速查表将你实际输入的 Postgres 命令汇集到一个可搜索的参考页面上:psql 元命令、查询、JSONB、数组和范围类型、CTE 和窗口函数、Upsert 和 RETURNING、数据类型、索引、角色和权限、维护和 EXPLAIN、备份和恢复,以及扩展。
它侧重于 Postgres 与其他数据库的不同之处——JSONB 运算符、数组和范围类型、带 RETURNING 的 ON CONFLICT upsert、GIN 和 GiST 索引,以及查询慢时你需要读懂的 EXPLAIN ANALYZE 输出——同时仍涵盖了日常的 SELECT、JOIN 和 DDL 语法。
本速查表免费且在客户端运行:使用搜索框实时过滤行,通过固定目录在章节间跳转,点击即可复制任何语句,还可以打印页面作为桌边参考。
如何使用 PostgreSQL 速查表
- 浏览各章节,从“psql 元命令”和“查询”到“索引”和“扩展”。
- 搜索关键词如 JSONB、ON CONFLICT 或 EXPLAIN 以实时过滤每一行。
- 需要存储和性能细节而非语法时跳转到“数据类型”或“索引”。
- 点击语句或其复制图标,将 SQL 复制到剪贴板。
- 打印本速查表获取离线 PostgreSQL 参考。
常见问题
十二个章节:psql 元命令、查询、JSONB、数组和范围类型、CTE 和窗口函数、Upsert 和 RETURNING、数据类型、索引、角色和权限、维护和 EXPLAIN、备份和恢复,以及扩展。
是的。专门的章节列出了 -> 和 ->> 访问器、@> 包含运算符、jsonb_set、jsonb_array_elements 以及使这些查询快速的 GIN 索引。
它们排在速查表的最前面——\l、\dt、\d、\du、\timing、\x 和其他你在写 SQL 之前就需要的反斜杠元命令。
可以。点击 SQL 代码片段或其行上的复制图标,即可立即复制到剪贴板。
是的,完全免费——可搜索、可打印,在浏览器中渲染,无需注册。
热门搜索
postgresql 速查表
psql 命令
postgres jsonb 查询
postgresql 数据类型
postgres upsert on conflict
postgresql 索引类型
postgres sql 参考手册
需要帮助?
使用此工具时遇到问题?请告诉我们的团队。