Docker Compose PostgreSQL Template

repository·main·Indexed 42 days ago

https://github.com/felipewom/docker-compose-postgres

A guide and template for setting up a PostgreSQL database environment using Docker Compose. Includes configurations for the postgres:17 image, data persistence via volumes, environment variable management using .env files, initialization scripts, and pgAdmin integration for web-based database management.

Tokens
1.7K
Snippets
9
Records
9
Agent score
3%

What's inside docker-compose-postgres

  1. Persist Postgres data using Docker volumes

    main

    By default, data in a container is ephemeral and lost when the container is deleted. To persist your database data on your local machine, use the volumes tag to map a local directory to the container's data path /var/lib/postgresql/data/.

    Use the ${PWD} variable to reference your current working directory.

    services:
      database:
        # ...
        volumes:
          - ${PWD}/db-data/:/var/lib/postgresql/data/
  2. Add pgAdmin service for database management

    main

    You can add a pgadmin service to your docker-compose.yml to manage your database via a web browser.

    Configuration details:

    • Access: Once running, access the UI at http://localhost:15433.
    • Connection: When registering a server in pgAdmin, use the name of your database service (e.g., database) as the Host name and 5432 as the Port.
    • Dependencies: Use depends_on to ensure the database starts before pgAdmin.

    Troubleshooting Permissions: If pgAdmin fails with a Permission denied: '/var/lib/pgadmin/sessions' error, ensure the mapped host folder is owned by user/group 5050:5050:

    mkdir -p pgadmin-data
    chown -R 5050:5050 pgadmin-data
    services:
      database:
        # ... (database config)
    
      pgadmin:
        image: dpage/pgadmin4
        ports:
          - 15433:80
        env_file:
          - .env
        depends_on:
          - database
        volumes:
          - ${PWD}/pgadmin-data/:/var/lib/pgadmin/
  3. Create a basic Postgres Docker Compose setup

    main

    To set up a PostgreSQL database using Docker Compose, create a docker-compose.yml file in your project root. This configuration uses the official PostgreSQL image and maps the default port 5432 to your host machine so you can connect to it from outside the container.

    Key configuration parameters:

    • image: Specifies the Postgres version (e.g., postgres:17).
    • ports: Maps [host_port]:[container_port].
    • environment: Sets POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB.

    Note: The service name (e.g., database) is used as the hostname when connecting from other services within the same Docker network.

    services:
      database:
        image: 'postgres:17'
        ports:
          - 5432:5432
        environment:
          POSTGRES_USER: username
          POSTGRES_PASSWORD: password
          POSTGRES_DB: default_database
  4. Use an .env file for Postgres configuration

    main

    Instead of hardcoding credentials in docker-compose.yml, you can use a .env file located at your project root.

    1. Create a .env file with your variables:
    POSTGRES_USER=username
    POSTGRES_PASSWORD=password
    POSTGRES_DB=default_database
    1. Update your docker-compose.yml to use the env_file tag:
    services:
      database:
        # ... other config
        env_file:
          - .env

    You can then remove the environment: block from your docker-compose.yml file.

  5. Run initialization scripts on database startup

    main

    You can automatically execute SQL scripts when the database is first initialized by mounting an .sql file to the /docker-entrypoint-initdb.d/ directory inside the container via a volume.

    services:
      database:
        # ...
        volumes:
          - ${PWD}/init.sql:/docker-entrypoint-initdb.d/init.sql
  6. Configure the pgAdmin service

    main

    The pgadmin service provides a web-based management interface for PostgreSQL using the dpage/pgadmin4 image.

    Key configurations:

    • Ports: Maps host port 15433 to container port 80.
    • Dependencies: Requires the database service to be running (depends_on: - database).
    • Environment: Loads configuration from a .env file.
    • Persistence & Permissions:
      • Session data is persisted in the host directory ${PWD}/pgadmin-data/.
      • Important: If startup fails with a permission error, you must set the host folder owner to 5050:5050 before starting.
    pgadmin:
        image: dpage/pgadmin4
        ports:
          - 15433:80
        env_file:
          - .env
        depends_on:
          - database
        networks:
          - postgres-network
        volumes:
          - ${PWD}/pgadmin-data/:/var/lib/pgadmin/
  7. Configure the Postgres database service

    main

    The database service runs a PostgreSQL instance using the postgres:17 image.

    Key configurations:

    • Ports: Maps host port 15432 to container port 5432.
    • Environment: Loads configuration from a .env file.
    • Persistence:
      • Database files are persisted in the host directory ${PWD}/db-data/.
      • Initialization scripts can be run by mounting an init.sql file from the host to /docker-entrypoint-initdb.d/init.sql.
    • Networking: Connects to the postgres-network.
    database:
      image: 'postgres:17'
      ports:
        - 15432:5432
      env_file:
        - .env
      networks:
        - postgres-network
      volumes:
        - ${PWD}/db-data/:/var/lib/postgresql/data/
        - ${PWD}/init.sql:/docker-entrypoint-initdb.d/init.sql
  8. Manage Docker Compose lifecycle (up/down)

    main

    Use the following commands to manage your Postgres environment:

    • Start the environment: Run this in the directory containing docker-compose.yml to build and start the services.
    • Stop the environment: Use this to shut down the containers and free up system resources. This does not delete your volumes (persisted data).
    # Start the database
    docker compose up
    
    # Stop the database
    docker compose down