PostgreSQL Integration with OpenClaw: Setup and Best Practices
Connect OpenClaw to PostgreSQL for persistent, production-ready database storage. Includes setup, connection pooling, and backup strategies.
PostgreSQL is the most popular choice for production OpenClaw apps. This guide covers setup, connection pooling, migrations, and backups.
Why PostgreSQL
- ACID compliance — reliable transactions
- JSON support — flexible schema when you need it
- Full-text search — built into the database
- Replication — scale reads with replicas
- Mature ecosystem — great client libraries
Option 1: Managed PostgreSQL
Use a managed provider for zero maintenance:
Neon
Serverless Postgres with auto-scaling. Scales to zero when idle. Best for: production apps with variable traffic.
openclaw db create neon mydb
Supabase
Postgres + auth + storage. Best for: SaaS apps that need user auth built-in.
Railway
Managed Postgres with a generous free tier. Best for: small to medium production apps.
Option 2: Self-Hosted on OpenClaw
openclaw db create postgres mydb
This spins up a Postgres container on your VPS. Good for:
- Development and staging
- Small production apps
- Teams that want full control
Connection Strings
OpenClaw provides a DATABASE_URL environment variable:
postgres://user:password@host:5432/database
Or for pooling:
postgres://user:password@host:5432/database?sslmode=require&pool_max_conns=10
Connection Pooling
Never connect directly from your app to Postgres without a pool. Use PgBouncer:
openclaw db pooling enable mydb --pool_mode=transaction
This reduces connection overhead and prevents "too many connections" errors.
Migrations
Use node-pg-migrate or Drizzle for schema migrations:
npm install node-pg-migrate
// migrations/001_create_users.js
exports.shorthands = undefined;
exports.up = pgm => {
pgm.createTable('users', {
id: 'id',
email: { type: 'varchar(255)', notNull: true, unique: true },
created_at: { type: 'timestamp', notNull: true, default: pgm.func('current_timestamp') }
})
};
exports.down = pgm => {
pgm.dropTable('users')
};
Run with:
node-pg-migrate up
Backups
Automated Backups (Neon, Supabase)
Managed providers handle backups automatically. Neon retains 7 days of point-in-time recovery.
Self-Hosted Backups
openclaw db backup mydb --schedule "0 2 * * *"
This runs daily at 2 AM and retains 7 backups.
Monitoring
Monitor your database with:
- Better Stack — query performance monitoring
- pg_stat_statements — slow query analysis
- OpenClaw dashboard — connection count, size, cache hit rate
Common Issues
Connection Timeout
Increase connect_timeout in your connection string:
...?connect_timeout=10
Too Many Connections
Enable connection pooling (see above). Also check for connection leaks in your app.
Slow Queries
Enable pg_stat_statements and look for queries with high total_exec_time.