PostgreSQL
databasepostgresqlTroubleshootingIntermediate
Common PostgreSQL Errors & Solutions
Troubleshoot and fix common PostgreSQL errors, authentication failures, connection pool exhaustion, unique constraint violations, and deadlocks.
Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#troubleshooting#debugging#errors#database
1. Password Authentication Failed
Error
FATAL: password authentication failed for user "app_user"Fix
- Verify the password matches.
- Check PostgreSQL's client authentication configuration file (
pg_hba.conf). Ensure the authentication method is set toscram-sha-256ormd5rather thanpeerfor network TCP connections:# /etc/postgresql/16/main/pg_hba.conf host all app_user 127.0.0.1/32 scram-sha-256 - Reload PostgreSQL:
sudo systemctl reload postgresql.
2. Connection Refused
Error
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" and accepting TCP/IP connections on port 5432?Fix
- Verify the PostgreSQL service is active:
sudo systemctl status postgresql. - Check
listen_addressesinpostgresql.conf:listen_addresses = '*' # Default is often 'localhost' - Verify port 5432 is open and listening:
ss -tulpn | grep 5432.
3. Duplicate Key Violates Unique Constraint
Error
ERROR: duplicate key value violates unique constraint "users_email_key"
DETAIL: Key (email)=(alice@example.com) already exists.Fix
Handle conflicts gracefully at the application level using ON CONFLICT DO NOTHING or ON CONFLICT DO UPDATE (upsert):
INSERT INTO users (email, username)
VALUES ('alice@example.com', 'alice_2')
ON CONFLICT (email) DO NOTHING;4. Connection Pool Exhaustion
Error
FATAL: remaining connection slots are reserved for non-replication superuser connectionsFix
- Increase
max_connectionsinpostgresql.conf(e.g.max_connections = 200). - Deploy PgBouncer or use application-side connection pooling (e.g. Prisma connection limits or
pg-pool) to multiplex database connections.
5. Deadlock Detected
Error
ERROR: deadlock detected
DETAIL: Process 12345 waits for ShareLock on transaction 67890; Process 67890 waits for ExclusiveLock on transaction 12345.Fix
- Ensure all concurrent transactions access and update tables in the exact same consistent order.
- Keep transaction durations as short as possible.
- Use
NOWAITorSET lock_timeout = '2s'to fail early rather than blocking indefinite deadlocks.
PostgreSQL Performance Optimization & Tuning
Analyze slow queries with EXPLAIN ANALYZE, tune memory parameters (shared_buffers, work_mem), manage autovacuum, and pool connections with PgBouncer.
Prisma ORM Overview
Introduction to Prisma — the next-generation TypeScript ORM featuring declarative data modeling, automatic type-safety, and seamless migrations.