DEV Community

abbazs
abbazs

Posted on

How to setup a postgres and pgadmin to be used at development and testing of a application using docker-compose?

Using docker compose we can achieve this. In the following docker-compose code we are binding the local folder to the folder in the docker for data persistence.

Copy the code to a file named docker-compose.yml and run the command docker-compose -f docker-compose.yml up

version: "3.5"

services:
  postgres:
    container_name: postgres_container
    image: postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
      POSTGRES_DB: ${POSTGRES_DB}
      PGDATA: /data/postgres
    volumes:
      - ./db_data:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: "False"
    volumes:
      # The local folder owner and group must be set to 5050
      # for the volume to work. Otherwise pgadmin is not able to
      # write to the local folder
      # sudo chown -R 5050:5050 pgadmin_data
      - ./pgadmin_data:/var/lib/pgadmin
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
gugaguichard profile image
Gustavo Guichard (Guga)

Hey Abbaz, great guide! If you're looking for a more streamlined way to manage PostgreSQL databases, check out Flashboard. Iā€™m one of the creators, and it offers a web-based UI that simplifies PostgreSQL management without additional setup like Docker. Would love to hear your thoughts!