Skip to content

Postgres

Configuration, extensions, and settings for running Postgres in production.

Postgres ships with conservative defaults tuned for broad compatibility. A few postgresql.conf changes and a handful of extensions cover most production needs.

Quick-start

-- run as superuser after install
ALTER SYSTEM SET shared_buffers        = '256MB';
ALTER SYSTEM SET work_mem              = '16MB';
ALTER SYSTEM SET maintenance_work_mem  = '256MB';
ALTER SYSTEM SET effective_cache_size  = '1GB';
ALTER SYSTEM SET random_page_cost      = 1.1;     -- SSD
ALTER SYSTEM SET effective_io_concurrency = 200;  -- SSD
ALTER SYSTEM SET log_min_duration_statement = 1000;

SELECT pg_reload_conf();

Adjust memory values to match available RAM; see the sections below for guidance.

Memory

shared_buffers

Postgres’s own page cache. The default (128 MB) is too low for most workloads. Set it to 25% of total RAM as a starting point.

ALTER SYSTEM SET shared_buffers = '4GB';  -- on a 16 GB machine

Postgres also benefits from the OS page cache, so giving it 100% of RAM is counterproductive.

work_mem

Memory available to each sort operation or hash table. The default (4 MB) causes spills to disk on even modest queries. The catch: a single complex query can allocate work_mem many times in parallel across multiple nodes and connections.

ALTER SYSTEM SET work_mem = '64MB';

A safer approach for write-heavy services is to leave the global setting conservative and raise it per-session for analytical queries:

SET work_mem = '256MB';
SELECT ...;

maintenance_work_mem

Used by VACUUM, CREATE INDEX, and ALTER TABLE. Raising this speeds up index builds and autovacuum significantly.

ALTER SYSTEM SET maintenance_work_mem = '1GB';

effective_cache_size

A planner hint, not an allocation. Set it to the total memory Postgres can expect to find in cache (shared_buffers + OS page cache), typically 75% of RAM. A higher value nudges the planner toward index scans over sequential scans.

ALTER SYSTEM SET effective_cache_size = '12GB';  -- on a 16 GB machine

Connections

max_connections

Each connection consumes memory even when idle. The default (100) is fine for most applications backed by a connection pool. Raising it blindly increases memory pressure and lock contention; fix connection pressure with pooling, not a higher limit.

Storage and WAL

random_page_cost and effective_io_concurrency

The default random_page_cost (4.0) assumes spinning disk. On SSDs, random reads are nearly as fast as sequential reads; lower this to 1.1 so the planner prefers index scans appropriately. Raise effective_io_concurrency to match SSD concurrency.

ALTER SYSTEM SET random_page_cost         = 1.1;
ALTER SYSTEM SET effective_io_concurrency = 200;

checkpoint_completion_target

Controls how aggressively Postgres spreads WAL writes across the checkpoint interval. The default (0.9) is already a good value; avoid setting it lower.

wal_level

Set to logical to enable logical replication and tools like pglogical or Debezium. replica (the default) supports physical standby replication only.

ALTER SYSTEM SET wal_level = 'logical';

Requires a server restart.

Observability

log_min_duration_statement

Logs any query that takes longer than N milliseconds. Set to 1000 in production to catch slow queries without flooding the log.

ALTER SYSTEM SET log_min_duration_statement = 1000;

Set to 0 to log every query (development only).

Extensions

Enable extensions once per database:

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS pgcrypto;

pg_stat_statements

Tracks execution statistics for every distinct query. Essential for identifying slow or high-frequency queries.

-- must be added to shared_preload_libraries; requires restart
ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_statements';
-- top 10 queries by total execution time
SELECT query, calls, total_exec_time, mean_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

pg_trgm

Trigram-based similarity matching. Enables fast LIKE and ILIKE index scans and fuzzy text search without a full-text search engine.

CREATE INDEX idx_users_email_trgm ON users USING gin (email gin_trgm_ops);

-- fuzzy match
SELECT * FROM users WHERE email % 'alice@exampl.com';

-- fast ILIKE
SELECT * FROM users WHERE email ILIKE '%alice%';

pgcrypto

Cryptographic functions: hashing, symmetric and asymmetric encryption, and random bytes.

-- one-way hash for passwords (use pgcrypto's crypt, not md5)
SELECT crypt('my_password', gen_salt('bf', 12));

-- verify
SELECT crypt('my_password', stored_hash) = stored_hash;

UUIDs

gen_random_uuid() returns a UUIDv4 and has been built-in since Postgres 13; no extension needed. Postgres 18 adds uuidv7(), which generates time-ordered UUIDs with better index performance.

-- Postgres 18+
public_id UUID NOT NULL DEFAULT uuidv7() UNIQUE

-- Postgres 13–17
public_id UUID NOT NULL DEFAULT gen_random_uuid() UNIQUE

For the reasoning behind UUID as an external identifier and the tradeoff between UUIDv4 and UUIDv7, see Identities.

Type to search…

↑↓ navigate↵ selectEsc close