Skip to main content
PostgreSQL is a powerful, open-source relational database system. Dokploy makes it easy to deploy and manage PostgreSQL instances as Docker services.

Creating a PostgreSQL Database

To create a PostgreSQL database in Dokploy:
  1. Navigate to your project
  2. Select the environment (e.g., Production, Staging)
  3. Click “Add Service” and select “PostgreSQL”
  4. 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 like postgres: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_DB
  • POSTGRES_USER
  • POSTGRES_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:
Example:

External Connection

If you’ve configured an external port (e.g., 5433):
Example:

Connection String Formats

Environment Variables

PostgreSQL containers support these environment variables: Dokploy automatically sets the required variables. You can add custom variables in the database settings.

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
The volume is named {appName}-data and persists even when the container is stopped or removed.

Resource Configuration

Configure resource limits for your PostgreSQL instance:
For production workloads, allocate at least 2GB of memory and 1 CPU core. Adjust based on your specific workload.

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

Rebuilding will delete all data. Make sure to backup first!

Accessing PostgreSQL CLI

To access the PostgreSQL CLI (psql) in the running container:
Or use the Dokploy terminal feature in the UI.

Common PostgreSQL Commands

Backups

Dokploy provides automated backup functionality for PostgreSQL. Backups are performed using pg_dump:
See the Backups page for detailed backup configuration.

Troubleshooting

Connection Refused

  • Verify the service is running: Check the status in Dokploy UI
  • Check the service name: Use the exact appName for 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

  1. Use Specific Versions - Pin to specific PostgreSQL versions (e.g., postgres:16.1) instead of latest
  2. Strong Passwords - Use strong, randomly generated passwords for production
  3. Regular Backups - Configure automated backups and test restoration
  4. Monitor Resources - Watch memory and CPU usage, adjust limits as needed
  5. Connection Pooling - Use connection pooling in your applications (e.g., pgBouncer)
  6. Read Replicas - For high-traffic applications, consider read replicas
  7. Maintenance Windows - Schedule regular maintenance for updates and optimization

Performance Tuning

Add these environment variables to tune PostgreSQL:
For more advanced configuration, mount a custom postgresql.conf file using volume mounts.

Next Steps

Backups

Configure automated PostgreSQL backups

Environment Variables

Learn about environment variable management