This post is part of a learning series: Quick Snippets

Setting Up a PostgreSQL Container: A Quick Guide

Posted by Vikash Patel on Saturday, Apr 1, 2023 (Updated Saturday, Dec 2, 2023) Reading time: 2 Min

Learn the simple and fast process of creating a PostgreSQL container using Docker. This guide provides both the command and a detailed explanation of the container and PostgreSQL database configuration.

1. Create the PostgreSQL Container:

docker run -d --name postgres \
  -e POSTGRES_PASSWORD=RunningFlying2 \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v postgres_data:/var/lib/postgresql/data \
  -p 5432:5432 \

2. Explanation:

The above command initiates the creation of a container with the latest version of PostgreSQL. Here’s a breakdown of the parameters used:

-d: Runs the container in detached mode.

--name postgres: Assigns the name "postgres" to the container.

-e POSTGRES_PASSWORD=RunningFlying2: Sets the PostgreSQL user password to "RunningFlying2".

-e PGDATA=/var/lib/postgresql/data/pgdata: Specifies the PostgreSQL data directory.

-v postgres_data:/var/lib/postgresql/data: Creates a Docker volume named "postgres_data" for persistent data storage.

-p 5432:5432: Maps the container's 5432 port to the host machine's 5432 port for external access.

postgres:latest: Pulls and uses the latest version of the PostgreSQL image.

3. Technical Details:

Container Name: postgres
Docker Volume: postgres_data
Port: 5432
User: postgres
Password: RunningFlying2

You have successfully set up a PostgreSQL container, ready to handle your database needs. The provided technical details ensure you can seamlessly connect to this container from your development or production environment.

Explore More:

Feel free to explore additional PostgreSQL configurations and features to tailor the container to your specific requirements.

Note: For enhanced security, consider changing default passwords in production environments.

Have an amazing day.

comments powered by Disqus