Home » Programming » Databases » How to Run PostgreSQL in a Docker Container in Linux

How to Run PostgreSQL in a Docker Container in Linux

This article will demonstrate how to run the PostgreSQL database service in a Docker container. Docker lets you run software in isolated environments that encapsulate specific configurations, software versions and dependencies separate from your main system (and each other). This makes it very useful for running multiple versions of the same program. There are many other reasons why you may wish to run PostgreSQL in a container – from ensuring consistency between systems, and running a version of PostgreSQL that is not available in your package manager.

Installing Docker

To use Docker, you’ll need to install it on your Linux system, as we detail in our instructions here.

Running a PostgreSQL Database Server in Docker

Once you have installed Docker, you can start pulling images (downloading the image from an image repository). Images are by default pulled from Docker’s public repository on Docker Hub.

Docker has an official PostgreSQL image which can be pulled by running:

docker pull postgres

Pulling a Specific Version of PostgreSQL

Docker images are organized by tags – labels that usually include the version of the primary software in the container, or other notable attributes of it such as additional packages that may be included.

By default, pulling an image will pull the most recent version, tagged latest.

To install a specific version of PostgreSQL, find the image on Docker Hub and click on the ‘View Available Tags‘ button – you’ll get a full list of available images for all versions of PostgreSQL, and the name of the image for that version. For example, to install PostgreSQL 15 specifically, run:

docker pull postgres:15

Running a Docker Image After Pulling It

Once the PostgreSQL image has been pulled (again, Docker lingo for downloaded)m it can be run using the following command:

docker run --name SERVER_NAME -e POSTGRES_PASSWORD=USER_PASSWORD -d postgres

Note that the following values should be replaced:

  • SERVER_NAME is a unique name for identifying running the PostgreSQL server container
  • USER_PASSWORD should be the password for the postgres user which will be used to connect to the server

More information can be found in the PostgreSQL documentation.

Connecting to the PostgreSQL Server Running in Docker

By default, the container will map it’s internal PostgreSQL port to the default PostgreSQL port on the local system (5432). Connect using the (psql command)[https://www.linuxscrew.com/postgresql-how-to-connect]:

psql -h localhost -p 5432 -U postgres

Stopping and Removing Docker Containers

Once you have finished with your database, the container can be stopped:

docker stop postgres

If you want to free up resources, you can also completely delete/destroy the container. WARNING, THIS WILL REMOVE ANY DATA YOU STORED IN THE DATABASES HOSTED ON THE POSTGRESQL SERVER WITHIN THE CONTAINER:

docker rm postgres

Where is My Data Stored When Working With Docker Containers?

By default, data (including PostgreSQL databases and their contents) are stored inside the container, and will be deleted with it if it is removed or recreated.

If you want your data to persist after removing a docker container, you will need to configure it to store data outside of the container – we cover how to do this in our article here.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment