How to Easily Install PostgreSQL Database with Docker

How to Easily Install PostgreSQL Database with Docker

PostgreSQL is a popular open-source relational database management system (RDBMS) known for its robustness and extensibility. Using Docker provides a quick and easy way to get a PostgreSQL database up and running, ensuring data persistence and flexibility. In this tutorial, we will guide you through the process of installing PostgreSQL using Docker and provide a docker-compose configuration to persist data on disk.

Prerequisites

Before we begin, ensure you have the following prerequisites:

  1. Docker installed on your system. If not, refer to our previous tutorial on installing Docker for your specific operating system.

Installing PostgreSQL with Docker

Step 1: Create an Environment Variables File

Create a directory for your project, navigate to it in your terminal, and create a .env file. This will contain environment variables that will be used by docker compose. It should contain the following:

POSTGRES_DB: postgres
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_PORT=5050

Make sure to replace myuser and mypassword with your desired username and password for accessing pgAdmin. By default the value is postgres

Step 2: Create a Docker Compose File

Let’s start by creating a docker-compose.yml file to define our PostgreSQL service. Create a new directory for your project, navigate to it in your terminal, and create the docker-compose.yml file using your favourite text editor. Here’s a sample docker-compose.yml configuration:

version: '3.8'
services:
  postgres:
    image: postgres:14-bullseye
    container_name: my_postgres_with_example_db
restart: always
    environment:
    POSTGRES_DB: ${POSTGRES_DB}
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
    - postgres_data:/var/lib/postgresql/data
    ports:
    - "${POSTGRES_Port}:5432"
volumes:
  postgres_data:

In this configuration:

  • We’re using the PostgreSQL v15 Docker image.
  • restart always allows the container to always attempt to restart your instance on failures. This is handy as it means your database will be available when you restart your computer.
  • We set environment variables for the database name, username, and password. Modify these values as needed.
  • We create a Docker volume (postgres_data) to persist the PostgreSQL data on disk.
  • Port 5432 is exposed to access the PostgreSQL database.

Step 3: Start PostgreSQL Container

Save the docker-compose.yml file, and in your terminal, navigate to the directory containing it. Run the following command to start the PostgreSQL container:

docker-compose up -d

The -d flag runs the container in detached mode, allowing you to continue using your terminal.

Step 4: Access PostgreSQL

You can now access the PostgreSQL database using a PostgreSQL client or by connecting to it from your application. Here’s how you can connect using the psql command-line tool:

docker exec -it my_postgres_with_example_db psql -U myuser postgres

Replace my_postgres_with_example_db, myuser, and postgres with your container name, username, and database name, respectively.

Step 5: Stopping and Removing the Container

When you’re done working with the PostgreSQL database, you can stop and remove the container using the following commands:

docker-compose down

This command will stop and remove the PostgreSQL container but will retain your data volume for future use.

Data Persistence

Thanks to the Docker volume (postgres_data), your PostgreSQL data is persistently stored on your host machine. This means that even if you stop and remove the PostgreSQL container, your data will be retained, ensuring data integrity and durability.

Wrapping Up

In this tutorial, we showed you how to easily install PostgreSQL with Docker using a docker-compose configuration that ensures data persistence on your local machine. This setup allows you to experiment with PostgreSQL, develop applications, and run database-driven services in a controlled and reproducible environment. Now you can start building and working with PostgreSQL databases in Docker with confidence. Happy coding!

Leave a Reply