website logo

Last Updated:

Running a PostgreSQL Database in Docker

We usually install PostgreSQL database in our local machine for development purpose. But once you have finished your development work, the PostgreSQL database is continuously running in the background and consuming your resources. If there is a better way to install and test databases in local machines?

Running a PostgreSQL Container in Docker

If you have installed Docker, you can install the PostgreSQL database image and run the container in your development lifecycle. Once you are done, you can just remove the container and chill. Sounds nice right. Let’s see the steps.

Getting the Database Image from Docker Hub

Navigate to the Docker Hub and choose what PostgreSQL image you want. For development purpose, I think postgres:latest image should be a good fit. Now let’s pull this image to your computer.

docker pull postgres:latest

Running a PostgreSQL container

Docker PostgreSQL container accepts many command line arguments. The only required argument is POSTGRES_PASSWORD. As the name implies, this argument set the password to your database.

By default, Postgres container create a default database called postgres and a default user postgres. You can change these two by setting the POSTGRES_USER argument. This argument add a new user and same named database.

If you only want to change the database name, just set the POSTGRES_DB argument. This change the default database from postgres to your desired name.

Now let’s run the Postgres Container.

docker container run -d -p 5432:5432 --name postgres-test -e POSTGRES_PASSWORD=password postgres:latest

As PostgreSQL runs on port 5432 by default, this command publishes this port to the host system.

If you want to make a one-off Postgres container and want to remove the container once it stops, use the --rm parameter.

docker container run --rm -d -p 5432:5432 --name postgres-test -e POSTGRES_PASSWORD=password -e POSTGRES_USER=hrishikesh postgres:latest

Getting inside a Running PostgreSQL Container

To get inside a running PostgreSQL container, we need to execute the bash command in the running container.

docker container exec -it postgres-test bash

Here, postgres-test is the name of the running container.

This command opens a bash prompt inside the container. The -it in this command means interactive TTY. That means, running the bash prompt in interactive mode.

Running PSQL inside a PostgreSQL Container

After you’re successfully getting into the container, run PSQL using the following command.

psql -h localhost -U postgres

Here, the -U means username. As the default username is postgres, I am using the same here. If you have provided a different username during running the container, you can use that username here.

Now you are in the PSQL. Use \h command to see all the options available to you. Start developing.

Removing the PostgreSQL Database container

Once your development is over, stop the container using the docker stop <containername> command. If you ran the container using the --rm argument, the container is automatically removed once it is stopped.

Otherwise, you can use the docker container rm <container name> command to remove the container manually.

To see all the container available in your system, use docker container ls -a command.

See Also