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の操作を1つの検索可能なリファレンスにまとめています。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、バックアップとリストア、拡張機能の12セクションです。
はい。専用のセクションで、->と->>のアクセサ、@>包含演算子、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 リファレンス
お困りですか?
このツールで問題が見つかりましたか?チームにお知らせください。