Technical, Data Analytics, Docker

How to run PostgreSQL in Docker

postgres docker

PostgreSQL is a powerful relational database management system. For local development, practice and learning one can run Postgres in docker container. This is a step-by-step guide how to run postgres in Docker.

This tutorial is for Linux/Ubuntu systems! If you have Docker installed already, then jump to Step 2

Step 1: Install Docker

  • Update your Linux package list:
Bash
sudo apt update
  • Install necessary prerequisites:
Bash
sudo apt install apt-transport-https ca-certificates curl software-properties-common
  • Add Docker’s official GPG key:
Bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • Add Docker’s official repository:
Bash
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • Update the Linux package list again:
Bash
sudo apt update
  • Install Docker(free community edition -ce):
Bash
sudo apt install docker-ce
  • Check Docker installation:
Bash
sudo systemctl status docker

This completes download and install of Docker in the system.

Step 1.1: Install Docker Compose (optional, if you want to use docker-compose)

  • Download the Docker Compose binary:
Bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • Apply executable permissions:
Bash
sudo chmod +x /usr/local/bin/docker-compose
  • Verify the installation:
Bash
docker-compose --version

Step 2: Pull the PostgreSQL Docker Image

  • Pull the PostgreSQL image from Docker Hub:
Bash
sudo docker pull postgres

here, change the Postgres version as you like. Executing this exact command will give you the full version

Step 3: Run PostgreSQL Container

Bash
sudo docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

OK, so in this part, replace my_postgres with your desired container name and mysecretpassword with a secure password of your choice, then take a note. Again this is for development, testing and learning purposes only. It is never recommended using Postgres in Docker for production, unless you exactly know what you are doing!

Step 4: Access PostgreSQL Container

  • List running Docker containers:
Bash
sudo docker ps
  • Access PostgreSQL inside the container:
Bash
sudo docker exec -it my_postgres psql -U postgres

Replace my_postgres with the name of your container.

Step 5: Persist Data (Optional)

Docker container naturally does not hold any data permanently. As soon as you stop the container, any data inside will be lost! To ensure data persistence, you can map a local directory to a directory in the container.

  • Stop and remove the current container:
Bash
sudo docker stop my_postgres
sudo docker rm my_postgres
  • Run PostgreSQL container with volume mapping:
Bash
sudo docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -v /path/to/your/local/directory:/var/lib/postgresql/data -d postgres

Replace /path/to/your/local/directory with the path to the directory on your host where you want to store the data. Also, don’t forget to replace my_postgres with your desirable container name and password!

Step 6: Manage PostgreSQL Service

  • Start PostgreSQL container:
Bash
sudo docker start my_postgres
  • Stop PostgreSQL container:
Bash
sudo docker stop my_postgres
  • Restart PostgreSQL container:
Bash
sudo docker restart my_postgres

That completes the installation of Postgres as Docker container.

Now interaction with the container/postgres

Interacting with your PostgreSQL container can be done in several ways, including using the psql command-line tool, connecting from an application, or using graphical database management tools. Here are the steps to interact with your PostgreSQL container:

  • Using the psql Command-Line Tool

You can use the psql tool to connect to your PostgreSQL database inside the Docker container. Make sure your container is running!

Access psql inside the container:

Bash
sudo docker exec -it my_postgres psql -U postgres

Replace my_postgres with the name of your container. This will start the psql tool and connect to the PostgreSQL instance running inside the container.

More resources:

Happy learning 🙂

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.