Creating a PostgreSQL Database
To create a PostgreSQL database in Dokploy:- Navigate to your project
- Select the environment (e.g., Production, Staging)
- Click “Add Service” and select “PostgreSQL”
- Configure the following settings:
Basic Configuration
- Name - A friendly name for your database
- App Name - The Docker service name (used for internal DNS)
- Description - Optional description
- Docker Image - Default:
postgres:latest(recommended to use specific version likepostgres:16)
Database Credentials
- Database Name - Name of the database to create
- Database User - Username for database access
- Database Password - Password for the user
These credentials are automatically configured via environment variables:
POSTGRES_DBPOSTGRES_USERPOSTGRES_PASSWORD
Advanced Settings
- External Port - Optional port to expose PostgreSQL externally (default internal port: 5432)
- Command - Override the default container command
- Args - Additional arguments to pass to PostgreSQL
- Environment Variables - Additional custom environment variables
Connection Strings
Internal Connection (from other services)
When connecting from applications within the same Docker network:External Connection
If you’ve configured an external port (e.g., 5433):Connection String Formats
- Standard URI
- psql Command
- Node.js (pg)
- Python (psycopg2)
Environment Variables
PostgreSQL containers support these environment variables:| Variable | Description | Required |
|---|---|---|
POSTGRES_DB | Database name | Yes |
POSTGRES_USER | Database user | Yes |
POSTGRES_PASSWORD | User password | Yes |
POSTGRES_INITDB_ARGS | Additional initdb arguments | No |
PGDATA | Data directory path | No |
Data Persistence
PostgreSQL data is stored in a Docker volume mounted at the data directory (path varies by image version):- PostgreSQL 14+:
/var/lib/postgresql/data - Older versions may use different paths
{appName}-data and persists even when the container is stopped or removed.
Resource Configuration
Configure resource limits for your PostgreSQL instance:Operations
Starting the Database
Click the “Start” button in the Dokploy UI or use the API:Stopping the Database
Click the “Stop” button or use the API:Reloading the Database
Reload applies configuration changes by stopping and starting the service:Rebuilding the Database
Accessing PostgreSQL CLI
To access the PostgreSQL CLI (psql) in the running container:Common PostgreSQL Commands
Backups
Dokploy provides automated backup functionality for PostgreSQL. Backups are performed usingpg_dump:
Troubleshooting
Connection Refused
- Verify the service is running: Check the status in Dokploy UI
- Check the service name: Use the exact
appNamefor internal connections - Verify network: Ensure your application is in the same Docker network
Authentication Failed
- Verify credentials in the database settings
- Check environment variables are correctly set
- Try recreating the database with correct credentials
Out of Memory
- Increase memory limits in resource configuration
- Check PostgreSQL logs for memory-related errors
- Consider optimizing queries or adding more resources
Data Volume Issues
- Check volume mounts in Docker:
docker volume ls - Inspect volume:
docker volume inspect {appName}-data - Verify mount path matches PostgreSQL version
Best Practices
- Use Specific Versions - Pin to specific PostgreSQL versions (e.g.,
postgres:16.1) instead oflatest - Strong Passwords - Use strong, randomly generated passwords for production
- Regular Backups - Configure automated backups and test restoration
- Monitor Resources - Watch memory and CPU usage, adjust limits as needed
- Connection Pooling - Use connection pooling in your applications (e.g., pgBouncer)
- Read Replicas - For high-traffic applications, consider read replicas
- Maintenance Windows - Schedule regular maintenance for updates and optimization
Performance Tuning
Add these environment variables to tune PostgreSQL:Next Steps
Backups
Configure automated PostgreSQL backups
Environment Variables
Learn about environment variable management