Quickly launch PostgreSQL via Docker Compose

Good day! Today I'll show you how to quickly and efficiently set up PostgreSQL using Docker Compose. This process will only take a few minutes, but the result will be a fully functional PostgreSQL database running in an isolated Docker container. Important components such as databases, tables and indexes will be stored on your local machine using volumes, ensuring data reliability and availability.

Preparation

Before you start deploying PostgreSQL on your local machine or VPS server, you need to install Docker and Docker Compose.

Docker is a platform for developing, delivering and operating applications in containers. Containers allow you to package applications with all their required dependencies into a single, standardized unit, providing isolation and repeatability in any environment.

Docker Compose is a tool for defining and running multi-container Docker applications. With it you can describe all the components of your application in a file docker-compose.yml and run them with one command. Docker Compose makes it easy to manage multiple containers by allowing you to define dependencies and network settings, and easily configure environment settings.

If you are new to using Docker, it is recommended that you start by installing Docker Desktop on your local machine. This app will automatically install Docker and Docker Compose, making it easy to get started with containerization.

Next I will show you an example setup docker-compose.yml file. This approach will not only allow you to quickly deploy PostgreSQL, but will also provide you with reliability in storing data, because it is based on volumes.

Step 1: Create a Docker Compose File

Create an empty directory and go to it. In the created directory we will create a file docker-compose.yml, which will describe our PostgreSQL service. Here is an example of my file:

version: '3.9'

services:
  postgres:
    image: postgres:latest
    container_name: postgres_container
    environment:
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: postgres_password
      POSTGRES_DB: postgres_db
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5430:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data/pgdata
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
    command: >
      postgres -c max_connections=1000
               -c shared_buffers=256MB
               -c effective_cache_size=768MB
               -c maintenance_work_mem=64MB
               -c checkpoint_completion_target=0.7
               -c wal_buffers=16MB
               -c default_statistics_target=100
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres_user -d postgres_db" ]
      interval: 30s
      timeout: 10s
      retries: 5
    restart: unless-stopped
    tty: true
    stdin_open: true

volumes:
  pgdata:
    driver: local

A quick overview of the Docker Compose file

  1. services/postgres:

    • image: the PostgreSQL Docker image used, in this case postgres:latest.

    • container_name: name of the container in which PostgreSQL will be run.

    • environment: environment variables for configuring PostgreSQL (user, password, database name – don't forget to include yours).

    • ports: port forwarding, where “5430:5432” means that the PostgreSQL port inside the container (5432) is forwarded to the host port (5430). This means that to connect to Postgres you will need to register port 5430.

    • volumes: mount local directory ./pgdata inside a container to save PostgreSQL data.

    • deploy: Defines the resources and deployment strategy for Docker Swarm (optional for standard Docker Compose use).

    • command: Additional PostgreSQL command line options for tuning performance parameters.

    • healthcheck: PostgreSQL health check using pg_isready.

    • restart, tty, stdin_open: settings for restarting the container and interacting with it through the terminal.

  2. volumes/pgdata:

Launching PostgreSQL

To deploy PostgreSQL using this Docker Compose file, run the following command in the directory containing the file docker-compose.yml (will work the same on both VPS and your local machine):

docker-compose up -d

This command will start the PostgreSQL container in the background (-d) based on the settings specified in the file docker-compose.yml.

If the container was deployed on a local machine, then “localhost” can be used as the host. Otherwise, use the IP address of the VPS server.

Conclusion

Using Docker Compose to deploy PostgreSQL greatly simplifies database management while providing great flexibility and reliability. The file described above docker-compose.yml contains basic settings for running PostgreSQL with performance settings, health monitoring, and resource management, making it an excellent choice for most projects.

If this information was useful to you, please let me know by liking, commenting or subscribing.

Thank you for attention.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *