Logo AppDev24 Login / Sign Up
Docker

Docker PostgreSQL Container

Updated on Aug 15, 2024 4 min Read

Are you looking to set up a PostgreSQL database container using Docker on your MacOS machine? Look no further! In this article, we'll walk you through the process of creating a Docker PostgreSQL container and testing it.

Step 1: Create an Environment File

The first step is to create an environment file that will store our database password.

.env

DB_PASSWORD=Password1234

This file will be used to set the POSTGRES_PASSWORD environment variable in our Docker container.

Step 2: Create a Docker Compose File

Next, we'll create a Docker compose file that defines our PostgreSQL container.

postgresql-docker-compose.yml

version: '3.9'

services:
  postgresql-db:
    # Apple M1 Chip
    # platform: linux/amd64
    image: postgres:14.8
    container_name: postgresql-db
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_PASSWORD: $DB_PASSWORD
    ports:
      - 5434:5432
    command:
      - "postgres"
      - "-c"
      - "wal_level=logical"
    volumes:     
      - postgresql_datadir:/var/lib/postgresql/data
    networks:
      - backend-network

networks:
  backend-network:
    driver: bridge

volumes:
  postgresql_datadir:

This file defines a single service, postgresql-db, which uses the official Postgres 14.8 image and maps port 5434 on your local machine to port 5432 in the container.

Step 3: Start the Container

Now that we have our environment file and Docker compose file set up, it's time to start the container! Run the following command:

docker-compose -f postgresql-docker-compose.yml up -d

This will start the container in detached mode, meaning it will run in the background.

Step4: Test PostgreSQL Database Container

Let us create a table & insert data. Note: you should have PostgreSQL Client installed locally.

First, set your password environment variable:

export PGPASSWORD=Password1234

Then, run the following commands to create a table and insert some data:

psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "CREATE TABLE consultants(id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,first_name VARCHAR(50),last_name VARCHAR(50),email VARCHAR(50),rate NUMERIC(8,2),status VARCHAR(20),created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "ALTER TABLE consultants REPLICA IDENTITY USING INDEX consultants_pkey;"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "INSERT INTO consultants(first_name, last_name, email, rate, status) VALUES ('John', 'Doe', 'john.doe@gmail.com', 3000.00, 'perm');"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "INSERT INTO consultants(first_name, last_name, email, rate, status) VALUES ('Tom', 'Hanks', 'tom.hanks@yahoo.com', 3500.75, 'contract');"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "INSERT INTO consultants(first_name, last_name, email, rate, status) VALUES ('Jane', 'Doe', 'jane.doe@moneybank.com', 3500.75, 'perm');"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "INSERT INTO consultants(first_name, last_name, email, rate, status) VALUES ('Duke', 'Johnson', 'duke@hello.com', 4500.25, 'contract');"
psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "INSERT INTO consultants(first_name, last_name, email, rate, status) VALUES ('Peter', 'Parker', 'peter@gmail.com', 4500.25, 'contract');"

Test the database objects:

psql -U postgres -h 127.0.0.1 -p 5434 -d postgres -c "SELECT * FROM consultants;"

Step 5: Stop the Container

Finally let us stop the Database Container

docker-compose -f postgresql-docker-compose.yml down

This will stop the container and remove it from memory.

That's it! We've successfully created and configured a Docker PostgreSQL container.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles

Comments

Join the Discussion.
Sign Up / Login