How to Run PostgreSQL on Docker


PostgreSQL is a powerful, open source object-relational database system. It is a popular choice for many applications and websites due to its robust feature set and scalability. Running PostgreSQL on Docker is a great way to quickly and easily deploy a PostgreSQL instance in a containerized environment. This tutorial will walk you through the steps of setting up and running PostgreSQL on Docker. We will cover how to install Docker, pull the PostgreSQL image, create a container, and configure the database. By the end of this tutorial, you will have a fully functioning PostgreSQL instance running on Docker.

How to Run PostgreSQL on Docker

1. Pull the PostgreSQL image from Docker Hub:

docker pull postgres

2. Create a Docker container from the PostgreSQL image:

docker run –name postgres-container -e POSTGRES_PASSWORD=mysecretpassword -d postgres

3. Connect to the PostgreSQL container:

docker exec -it postgres-container psql -U postgres

4. Create a database:


5. Create a user:

CREATE USER myuser WITH PASSWORD ‘mypassword’;

6. Grant privileges to the user:


7. Exit the PostgreSQL container:


PostgreSQL, also referred to as Postgres, is an open-source, object-relational database management system. Developers often opt for this relational database as it is free, stable, and flexible. In fact, PostgreSQL and MySQL are the most popular Relational Database Management Systems.

Today, Postgres is one of the most widely used Docker images that run in containers. The popularity of containerized databases is attributed to the simplicity with which they can be deployed. Also, instead of having one centralized database for many applications, developers can devote a PostgreSQL container for each application.

In this tutorial, you will learn how to run PostgreSQL on a Docker container.

Tutorial on how to run PostgreSQL in a Docker container.


  • Access to a command line/terminal window
  • A user account with sudo privileges
  • An existing Docker installation

Run PostgreSQL on Docker Containers

Deploying a Postgres container is simple. You can find the Postgres image for building these database containers in Docker’s official repository.

This guide shows you two ways to do so.

The first option uses Docker Compose, a tool for managing multi-container Docker applications. You can use Docker Compose to configure the Postgres as a service running inside of a container. In that case, you create a yaml file with all the specifications.

Alternatively, you can use a single Docker command with all the necessary information for deploying a new PostgreSQL container.

Option 1: Run Postgres Using Docker Compose

To deploy a Postgres container using Docker Compose, you should have this Docker tool set up on your system.

If you are a Linux user and need help setting up, refer to one of our guides on installing Docker Compose on Ubuntu or how to install Docker Compose on CentOS.

1. To ensure an easy and clean installation, we first want to create a working directory named postgres and move into that directory:

mkdir postgres
cd postgres/

2. Next, use Docker Compose to download the Postgres image and get the service up and running. Do this by creating a new docker-compose.yml file with an editor of your choice (in this example, we used nano):

nano docker-compose.yml

3. Add the following content to the docker-compose file:

version: ‘3’
  image: ‘postgres: latest’
    - “5432:5432” 

The yaml configuration file outlines there is a postgres service, built on the latest postgres image. You can decide on the newest Postgres version or specify the version number you want to use.

Finally, you need to define the ports on which the container communicates. 5432 is the default port number for PostgreSQL.

4. Save and exit the file.

5. Now that you have the yaml configuration file, you can start the postgres service and run the container. Use the docker-compose up command with the -d option to put it into detach mode (allowing you to continue to run commands from the current shell):

docker-compose up -d

6. You can check the logs with the command:

docker-compose logs -f

To return to the shell press CTRL+C.

Option 2: Run Postgres Using a Single Docker Command

Another way to deploy PostgreSQL in a container is by running a single docker command

1. You can download and run a Postgres container by specifying all the necessary information in one command.

docker run --name [container_name] -e POSTGRES_PASSWORD=[your_password] -d postgres

The command tells Docker to run a new container under a particular container name, defines the Postgres password, and downloads the latest Postgres release.

2. Confirm your PostgreSQL container is now up by prompting Docker to list all running containers with:

docker ps

In this guide, we created a container named example and we can quickly locate it among other running containers.

A single command for deploying Postgres in a Docker container.

Starting with Postgres Containers

Connect to Postgres in Docker Container

To enter a Postgres container, you need to execute using the container name and enable psql, the command-line interface for Postgres.

docker exec -it [container_name] psql -U [postgres_user]

In the example below, we connected to the example container as the postgres user.

Command for connecting to Postgres in a Docker container.

Create a Database

1. Once in the Docker Postgres container, we can create a database with:

create database [db_name];

Note: To view all the databases you have running on PostgreSQL run: \l.

2. Connect to the database as the postgres user type:

\c [db_name]

3. With the database set up, the next step is to create a schema that helps you get a logical representation of the database structure:

create schema [db_schema_name]

4. Here you can create a table and add data into the table.

create table [table_name] ([field_names] [values])

Note: If you want to exit the Postgres container, type in the following: \q.


In this article you learned two different ways to run a PostgreSQL in a Docker container. After deploying a Docker Postgres container, you can start creating your databases with all the data required. Deploying PostgreSQL in a container is cost-efficient in terms of infrastructure, it also supports CI/CD development, and streamlines deployment and application management.

If you want to learn more about PostgreSQL deployment, make sure to read our article how to deploy PostgreSQL on Kubernetes.

Beside PostgreSQL you can also run MySQL and MongoDB on Docker.


How to Run PostgreSQL on Docker

PostgreSQL is a powerful, open source object-relational database system. It is a popular choice for many small and large projects, and it has a great set of features geared towards reliability, data integrity, and robustness. With Docker, you can easily deploy and run PostgreSQL in a matter of minutes.

Step 1: Install Docker

The first step is to install Docker on your system. You can find instructions for installing Docker on the official Docker website. Once you have Docker installed, you can move on to the next step.

Step 2: Pull the PostgreSQL Image

The next step is to pull the PostgreSQL image from the Docker Hub. You can do this by running the following command:

docker pull postgres

This will pull the latest version of the PostgreSQL image from the Docker Hub. Once the image is downloaded, you can move on to the next step.

Step 3: Run the PostgreSQL Container

Now that you have the PostgreSQL image downloaded, you can run the container. To do this, you can use the following command:

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

This will create a container named “postgres” and set the password to “mysecretpassword”. You can change the password to whatever you like. Once the container is running, you can move on to the next step.

Step 4: Connect to the PostgreSQL Container

Now that the PostgreSQL container is running, you can connect to it. To do this, you can use the following command:

docker exec -it postgres psql -U postgres

This will connect you to the PostgreSQL container as the “postgres” user. Once you are connected, you can start running SQL commands.


Running PostgreSQL on Docker is a great way to quickly deploy and run a PostgreSQL database. With just a few commands, you can have a PostgreSQL database up and running in minutes. Give it a try and see how easy it is!

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.


Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00