Getting started with PostgreSQL using Docker Compose

By sanjeev

Sanjeev kumar

March 30, 2025

Published

In today's tech landscape, efficient and scalable data management is crucial for businesses and developers alike. PostgreSQL, a powerful open-source relational database management system, provides the tools necessary to handle complex data requirements with ease.

In this blog post, we'll explore how to set up PostgreSQL using Docker Compose, a tool that simplifies the deployment and management of containerized applications. By the end of this guide, you'll have a PostgreSQL instance up and running in no time, ready to support our event-scheduler applications' data needs.

postgres-docker-image.webp

Prerequisites

Before we dive into the setup process, make sure you have Docker and Docker Compose installed on your system. If you haven't installed them yet, you can follow the official installation guides for Docker and Docker Compose.

Setting Up PostgreSQL with Docker Compose

Step 1: Create a Docker Compose file

Create a new directory for your PostgreSQL setup and navigate into it. Then, create a file named docker-compose.yml and open it in your favorite text editor.

version: '3.8'

services:
  postgres:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data: 

In this Docker Compose file:

  • We define a service named postgres using the official PostgreSQL Docker image.

  • We specify environment variables for the database name, username, and password.

  • Port 5432 is exposed to allow connections to the PostgreSQL server.

  • A volume is created to persist data in the database.

Step 2: Run Docker Compose

Save the docker-compose.yml file and run the following command in your terminal to start PostgreSQL:

docker-compose up -d

Docker Compose will download the PostgreSQL image (if not already downloaded) and create a container based on the configuration specified in the docker-compose.yml file.

Step 3: Verify PostgreSQL Setup

To verify that PostgreSQL is up and running, you can use the psql command-line tool provided by PostgreSQL. Run the following command to connect to the PostgreSQL server:

docker-compose exec postgres psql -U myuser mydatabase

You'll be prompted to enter the password specified in the Docker Compose file. Once authenticated, you'll have access to the PostgreSQL command-line interface.

# To list all the DBs in the container, enter the meta-daata command "\l" (list)

mydatabase-# \l 

                                                    List of databases
    Name    | Owner  | Encoding | Locale Provider |  Collate   |   Ctype    | ICU Locale | ICU Rules | Access privileges 
-----------+--------+----------+-----------------+------------+------------+------------+-----------+-------------------
 mydatabase | myuser | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | 
 postgres   | myuser | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | 
 template0  | myuser | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | =c/myuser        +
            |        |          |                 |            |            |            |           | myuser=CTc/myuser
 template1  | myuser | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | =c/myuser        +
            |        |          |                 |            |            |            |           | myuser=CTc/myuser
(4 rows)

Step 4: Interact with PostgreSQL

With PostgreSQL running, you can now interact with it using your preferred tools and programming languages. Whether you're developing a web application, mobile app, or anything in between, PostgreSQL's versatility and reliability make it an ideal choice for storing and managing your data.

In this tutorial, we've covered the basics of setting up PostgreSQL using Docker Compose. By leveraging the power of containers, you can quickly spin up PostgreSQL instances for development, testing, or production environments with ease. As you continue to explore PostgreSQL, don't hesitate to dive deeper into its features and capabilities to unlock its full potential for your projects.

Let us continue with our event scheduler app to connect to the postgres db and create an auth module.

Share This Article

Share this post

Help others discover this content by sharing it with your network