DEV Community

DEVESH MASANE
DEVESH MASANE

Posted on

Docker Compose: Quick Local Development Setup - Postgresql

Hello everyone,

Sharing a quick setup guide for PostgreSQL docker-compose to get your local development started.

Prerequisite: Docker & docker-compose installed

Let's Start
Please create a new yml file(I created postgres-local.yml).

Select the version of Postgres from Dockerhub that you wish to use. I will be using:

  • postgres:16.2-alpine

Edit the yml file

services:
  local-postgres-service:
    container_name: local-postgresql
    image: postgres:16.2-alpine
Enter fullscreen mode Exit fullscreen mode

local-postgres-service - Name of service
local-postgresql - Name of docker container
postgres:16.2-alpine - postgres image with tag from Dockerhub

Now, we need to set the environment variables for our PostgreSQL

services:
  local-postgres-service:
    container_name: local-postgresql
    image: postgres:16.2-alpine
    environment:
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: pass123
      POSTGRES_DB: local-db
Enter fullscreen mode Exit fullscreen mode

postgres_user - username
pass123 - password
local-db - database name

To connect with this postgres container we need to bind port. Postgres container will run at port 5432, we will bind this to port 5000.

services:
  local-postgres-service:
    container_name: local-postgresql
    image: postgres:16.2-alpine
    environment:
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: pass123
      POSTGRES_DB: local-db
    ports:
      - "5000:5432"
Enter fullscreen mode Exit fullscreen mode

We need to make the data persistent using docker volumes.

services:
  local-postgres-service:
    container_name: local-postgresql
    image: postgres:16.2-alpine
    environment:
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: pass123
      POSTGRES_DB: local-db
    ports:
      - "5000:5432"
    volumes:
      - local-postgres-volume:/var/lib/postgresql/data

volumes:
  local-postgres-volume:
Enter fullscreen mode Exit fullscreen mode

local-postgres-volume - volume name where data will persist
/var/lib/postgresql/data - path of data inside postgres container

Finally, let's add this to a docker network

services:
  local-postgres-service:
    container_name: local-postgresql
    image: postgres:16.2-alpine
    environment:
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: pass123
      POSTGRES_DB: local-db
    ports:
      - "5000:5432"
    volumes:
      - local-postgres-volume:/var/lib/postgresql/data
    networks:
      - local-postgres-network

volumes:
  local-postgres-volume:

networks:
  local-postgres-network:
Enter fullscreen mode Exit fullscreen mode

local-postgres-network - docker network name

Start the container in detach mode using:
docker compose -f postgres-local.yml up -d
Stop the container using:
docker compose -f postgres-local.yml stop

Let's check the connection to our postgres container

Image description

Thank you for reading !!!

Top comments (0)