Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

Costas Zarifis


Tutorials, code snippets, hacks and more...

Postgres and pgAdmin4 docker communication

Before you proceed with these steps, make sure you've successfully installed docker on your system.

Obtain the postgres docker by running the following command:

docker pull postgres:latest

After the image has been obtained launch it by using the following command:

docker run -d -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 --restart=always postgres

Note that this command specifies that the username/password used to connect to the database is: postgres/postgres

By running the command:

docker ps

We can now see the dockers that are currently up and running.

By running the command:

 docker inspect <CONTAINER_ID>

We can see additional information about the docker image with the respective CONTAINER_ID. The CONTAINER_ID of a particular image can be obtained by running the command `docker ps'

Once you run the `docker inspect' command, take note of the IP address of the docker (e.g., "IPAddress": "172.17.0.3")

The next step is to obtain a docker that contains PG-Admin which is the client we will be using to connect to the database.

This can be automatically performed by running the command:

docker run --rm -p 5050:5050 thajeztah/pgadmin4

Once this process has finished, open your browser and connect to the address localhost:5050

Click on the button: "Add New Server", once clicked a popup will appear. Type a Name on the first field (i.e., mydb), then click on "Connection".

In the new tab window type the IP Address we took note of earlier (in this example I will be using the IP: 172.17.0.3), and type the password "postgres" in the appropriate field, while leaving all other fields unchanged. Once done click on "Save".

Congratulations! You've been successfully connected to the postgres database.

NOTE! Please keep in mind that:

  • The postgres docker image will keep running until you explicitly kill it. You can kill it by using the command docker kill < CONTAINER_ID >.
  • Once the postgres docker image is terminated ALL DATA WILL BE LOST (including the database, tables and so on). You can either commit the docker image so that everything will be saved or you can simply dump the contents into a file and load it next time the docker is launched.

About the author

Costas Zarifis


Discussions

comments powered by Disqus