Run PostgreSQL and pgAdmin in docker for local development using docker compose

  • create a directory

    mkdir postgres-docker && cd postgres-docker

  • create a file and name it as docker-compose.yml

  • Add the following content in the docker-compose,yml file
version: "3.8"
services:
  db:
    image: postgres
    container_name: local_pgdb
    restart: always
    ports:
      - "54320:5432"
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: admin
    volumes:
      - local_pgdata:/var/lib/postgresql/data
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin4_container
    restart: always
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: raj@nola.com
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - pgadmin-data:/var/lib/pgadmin

volumes:
  local_pgdata:
  pgadmin-data:
  • First line defines the version of the compose file which is 3.8. You can read more from Docker’s documentation.
  • Next we have  services section. Inside this, we have to define 2 services postgreSQLand pgAdmin
  • container_name is used to define container names for postgreSQL & pgAdmin, the default values are overridden by local_pgdb and pgadmin4_container values
  • restart always will restart the container when either the Docker daemon restarts or the container itself is manually restarted

  • ports is used to define both host and container ports. It maps port 54320 on the host to port 5432 on the container for postgreSQL and 5050 on the host to port 80 on container for pgAdmin
  • environment defines a set of environment variables , for both services we have set the user id and password.
  • volumes tag is used to mount a folder from the host machine to the container. Its the path in the host where the database data is stored. The above config will create volume name local_pgdata and mount this volume to container’s path.
  • Inorder for pgAdmin to persist the connection details even after the container is shutdown , we need to provide volume and that is provided under volumes section. It has to be /var/lib/pgadmin. I was not able to make docker persist the connect details with any other path
  • Docker volumes are the recommended way to persist data. When you stop or down the docker container, the database and connection details will still be there.
  • Now run the below command to run postgreSQL and pgAdmin4 in a detached mode
  • docker compose up -d
  • If all goes well, then below message will be displayed
  • To view the logs , use command docker logs -f local_pgdb
  • To configure pgadmin – open a browser and go to – http://localhost:5050/ . In the connection details for hostname give the container name of postgreSQL

  • This will allow pgAdmin to connect to the postgreSQL database .

    Thank you !! Please share your queries and feedback in the comments section.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s