Say Hi

Setup and Install PostgreSQL Using Docker

April 18, 2022

Docker is an excellent tool for simplifying the installation and management of applications, including PostgreSQL. By using Docker, you can effectively isolate PostgreSQL from your system and avoid potential conflicts with other installations or configurations.

In this post, we will discuss how to install and run PostgreSQL using Docker. We will not be going in the details of Docker installation so make sure to have it installed and running on your system. Also, the aim of this post is to provide you with a quick guide for local development purposes.

Step 1 — Pull the Docker Image

Start by pulling the latest official PostgreSQL image from Docker Hub:

docker pull postgres

Step 2 — Run the Container

Now that you have the PostgreSQL image, run a new Docker container with the following command:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

Replace some-postgres with a custom name for your PostgreSQL container and mysecretpassword with a secure password. This command will create and start a new PostgreSQL container.

Step 3 — Connect to Container

To connect to the running PostgreSQL container, you can use the following command:

docker exec -it some-postgres psql -U postgres

Replace some-postgres with the name of your PostgreSQL container. You should now be connected to your PostgreSQL instance and able to run SQL commands.

Step 4 — Persisting Data

By default, all data stored within the PostgreSQL Docker container will be removed when the container is deleted. To persist data, add a volume to your container using the -v flag:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -v /path/to/host/folder:/var/lib/postgresql/data -d postgres

Replace /path/to/host/folder with the directory path on your host machine where you would like the data to be stored.

Step 5 — Accessing Remotely

To access your PostgreSQL container remotely, you’ll need to publish the port on which it’s running. The default PostgreSQL port is 5432. Use the -p flag to publish the port:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

Now you can connect to your PostgreSQL container using any PostgreSQL client by providing the host IP address or using the localhost address if you are connecting from the same machine and the given port.

Conclusion

Using Docker is a convenient and efficient way to install and manage PostgreSQL. By utilizing containers, you can easily control your PostgreSQL resources and maintain database isolation. Following the above steps, you can quickly install, set up, and access PostgreSQL using Docker.