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:
sudo apt update
- Install necessary prerequisites:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s official GPG key:
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:
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:
sudo apt update
- Install Docker(free community edition -ce):
sudo apt install docker-ce
- Check Docker installation:
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:
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:
sudo chmod +x /usr/local/bin/docker-compose
- Verify the installation:
docker-compose --version
Step 2: Pull the PostgreSQL Docker Image
- Pull the PostgreSQL image from Docker Hub:
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
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:
sudo docker ps
- Access PostgreSQL inside the container:
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:
sudo docker stop my_postgres
sudo docker rm my_postgres
- Run PostgreSQL container with volume mapping:
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:
sudo docker start my_postgres
- Stop PostgreSQL container:
sudo docker stop my_postgres
- Restart PostgreSQL container:
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:
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 🙂