Postgres Database and PG Admin using Docker Compose

Docker makes all development things really easy. Just one line command and you are up with whatever you want (of course if it’s available as docker Image ๐Ÿ˜‰).

Gone are those days when you have to spend hour or so to setup just a simple database on you local development environment , worrying about process , ports etc. Docker makes it really easy for you . In this article we will use a simple docker compose file to setup the PostgreSQL data base as well as PG Admin.

Here is the docker compose file to set it up. Save this code as docker-compose.yaml

version: "3"
    container_name: my-pg-db
    image: postgres:13.4-buster
    restart: always
      POSTGRES_USER: pguser
      POSTGRES_PASSWORD: pgpassword
      POSTGRES_DB: mydatabase
      - "5432:5432"
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data
    container_name: my-pgadmin
    image: dpage/pgadmin4
    restart: always
      - "9080:80"

To run this docker container go to the folder in which it is saved and type the following command

$ docker-compose up

The above command will run 2 docker containers , one is postgres db container my-pg-db and other one is PG Admin container my-pgadmin. You can check these with docker ps command . Now as the PG Admin container is running at port 9080 got to http://localhost:9080 , you should see page like this.

Here you have to login with credentials given in my-pgadmin container . One login you will have to add server from left side

In create Server page , in general section provide any name

In Connection Tab as shown you will have to provide IP address of the my-pg-db container.

to get IP address of the container , type below command

$ docker inspect <CONTAINER_ID_OF_my-pg-db> | grep IPAddress

the IP address returned by this command should be used at Host Name/address field .
Other fileds are similar to environment variables provided to my-pg-db in you docker-compose.yaml file. Click on save and you will see the database listed in you server.

To access this database from your development environment just use IP as localhost and port as mention in docker file (int this case 53432)

for example your JDBC url should look like for postgres database


That’s it , it is as simple as that .