Docker and PostgreSQL make a powerful combination for development environments. With this setup, you can create consistent, isolated database environments that are easy to manage, version, and share with your team. Let’s explore how to create the ultimate PostgreSQL development environment using Docker.
Why Use PostgreSQL with Docker?
- Isolation: Keep your database separate from your system
- Consistency: Everyone on your team works with identical database environments
- Portability: Works the same across macOS, Windows, and Linux
- Version control: Easily switch between PostgreSQL versions
- Quick reset: Tear down and rebuild your database environment in seconds
Setting Up PostgreSQL with Docker
Step 1: Install Docker
First, make sure you have Docker installed on your machine. Visit Docker’s official website to download and install Docker Desktop for your operating system.
Step 2: Create a Docker Compose File
Create a file named docker-compose.yml
in your project directory:
version: '3.8'
services:
postgres:
image: postgres:15
container_name: my_postgres
restart: always
environment:
POSTGRES_USER: devuser
POSTGRES_PASSWORD: devpassword
POSTGRES_DB: devdb
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init-scripts:/docker-entrypoint-initdb.d
volumes:
postgres_data:
Step 3: Add Initialization Scripts (Optional)
Create a directory called init-scripts
in your project folder. Any SQL files placed here will automatically run when the container starts for the first time.
Example init-scripts/01-schema.sql
:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Add some test data
INSERT INTO users (username, email) VALUES
('testuser', 'test@example.com'),
('devuser', 'dev@example.com');
Step 4: Start Your PostgreSQL Container
Run the following command in your project directory:
docker-compose up -d
Your PostgreSQL server is now running and accessible at localhost:5432!
Advanced Configuration
Environment Variables
Customize your PostgreSQL instance with environment variables in the docker-compose.yml file:
environment:
POSTGRES_USER: devuser
POSTGRES_PASSWORD: devpassword
POSTGRES_DB: devdb
PGDATA: /var/lib/postgresql/data/pgdata
# Performance tuning
POSTGRES_SHARED_BUFFERS: 256MB
POSTGRES_WORK_MEM: 16MB
POSTGRES_EFFECTIVE_CACHE_SIZE: 1GB
Persistent Data
The volume postgres_data
ensures your data persists between container restarts.
Multi-Database Setup
Need multiple databases? Modify your init script:
-- init-scripts/00-create-databases.sql
CREATE DATABASE app_test;
CREATE DATABASE app_dev;
Connecting to Your PostgreSQL Container
Using psql from Host
docker exec -it my_postgres psql -U devuser -d devdb
Using psql from Within Container
docker exec -it my_postgres bash
psql -U devuser devdb
Using a GUI Client
Connect using tools like pgAdmin, DBeaver, or DataGrip with these settings:
- Host: localhost
- Port: 5432
- Database: devdb
- Username: devuser
- Password: devpassword
Best Practices
- Never use default passwords in production environments
- Version your database schema using migration scripts
- Back up your data regularly using
pg_dump
- Use separate containers for development, testing, and staging
- Add healthchecks to ensure your database is ready before your application connects
Troubleshooting
- Port conflicts: If port 5432 is already in use, change the port mapping in your docker-compose.yml
- Permission issues: Check file permissions for mounted volumes
- Container won’t start: View logs with
docker-compose logs postgres
Conclusion
A Docker-based PostgreSQL setup offers flexibility, consistency, and ease of use for development. By following this guide, you’ve created a powerful development environment that can be shared across your team and easily modified as your project grows.
Happy coding with PostgreSQL and Docker!
Leave a Reply