Postgres and pgAdmin in Docker Compose

Setup Guide — PostgreSQL Series, Episode 1

J3
Jungletronics
Published in
3 min readNov 17, 2024

In Episode 1, we dive into setting up PostgreSQL and pgAdmin using Docker Compose. Building on the foundation from Episode 0, this guide simplifies container orchestration, enabling seamless communication between PostgreSQL and pgAdmin. Whether you’re following a learning path like J. Portilla’s SQL Bootcamp or seeking an efficient environment for SQL development, Docker Compose offers a streamlined solution for managing both services effortlessly.

Let’s Get Started:

0. Step : Cleaning & Pulling Images & Permissions


docker rm -f $(docker ps -a -q)
docker system prune -a

docker pull postgres
docker pull dpage/pgadmin4

mkdir -p /home/j3/Documents/pg_db_2
touch /home/j3/Documents/pg_db_2/docker-compose.yml
sudo chown -R j3:j3 /home/j3/Documents/pg_db_2

1. Step :Docker Compose Setup (Optional)

If you want to make it easier to manage your pgAdmin container and PostgreSQL container together, you can create a docker-compose.yml file. Here’s an example:

/home/j3/Documents/pg_db_2/docker-compose.yml


services:
pgadmin_service:
image: dpage/pgadmin4
container_name: my-pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: giljr.2009@gmail.com
PGADMIN_DEFAULT_PASSWORD: postgres
ports:
- "15432:80"
networks:
- my-network
volumes:
- ./pgadmin-data:/var/lib/pgadmin

postgres_service:
image: postgres
container_name: my-postgres
environment:
POSTGRES_USER: postgres:17
POSTGRES_PASSWORD: postgres
TZ: America/New_York
ports:
- "5433:5432"
networks:
- my-network

networks:
my-network:
driver: bridge

This way, both pgAdmin and PostgreSQL are part of the same network, and you can manage them with a single command:

docker compose up -d --build

2. Step : Verify Containers Are Running

List running Docker containers to ensure both are active:

docker ps

You should see bothmy-pgadmin and my-postgres containers listed.

CONTAINER ID   IMAGE            COMMAND                  CREATED              STATUS          PORTS                                                NAMES
99be8bf50b23 dpage/pgadmin4 "/entrypoint.sh" 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:15432->80/tcp, [::]:15432->80/tcp my-pgadmin
9757d7da5eff postgres "docker-entrypoint.s…" About a minute ago Up 59 seconds 0.0.0.0:5433->5432/tcp, [::]:5433->5432/tcp my-postgres

Visit :

http://localhost:15432/

By the end of Episode 1, you’ve successfully set up PostgreSQL and pgAdmin using Docker Compose, creating a robust and efficient development environment. This setup not only simplifies container management but also lays the foundation for exploring more advanced SQL workflows in future episodes. With this streamlined system in place, you’re now ready to dive deeper into database management and make the most of your PostgreSQL journey.

There you have it!

Congrats!

See you in the next episode!

For more information see Episode 0!

Note:

To modify the timezone in a PostgreSQL container running in Docker, follow these steps:

1. Modify the Timezone Inside the Container

PostgreSQL uses the system’s timezone configuration. If you want to set a specific timezone for the container:

👉Option 1: Set Timezone at Runtime

Pass the timezone as an environment variable when running the container:

docker run -d \
— name postgres \
-e TZ=America/New_York \
-e POSTGRES_PASSWORD=mysecretpassword \
postgres:17

Here:

TZ: America/New_York sets the container’s timezone.

👉Option 2: Manually Update Timezone in a Running Container

Access the running container:

docker exec -it postgres bash

Set the timezone:

ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
echo “America/New_York” > /etc/timezone

Restart the PostgreSQL service inside the container:

service postgresql restart

2. Update PostgreSQL’s Default Timezone

You can set PostgreSQL’s timezone configuration directly:

👉Option 1: Modify postgresql.conf

Access the container:

docker exec -it postgres bash

Edit the postgresql.conf file (usually in /var/lib/postgresql/data or /etc/postgresql/17/main):

timezone = ‘America/New_York’

Restart the container:

docker restart postgres

👉Option 2: Use SQL Commands

Connect to PostgreSQL inside the container and set the timezone at the database or session level:

Session Level:

SET TIME ZONE ‘America/New_York’;

Database Level:

ALTER DATABASE my_database SET TIME ZONE ‘America/New_York’;
3. Verify the Timezone

3. Verify the Timezone

To confirm the timezone configuration:

Check the container’s timezone:

docker exec -it postgres date

Check PostgreSQL’s timezone:

SHOW TIME ZONE;

4. Persisting Timezone in Docker Compose

If you’re using Docker Compose, add the timezone setting in the docker-compose.yml file:

version: ‘3.8’
services:
postgres:
image: postgres:17
environment:
POSTGRES_PASSWORD: mysecretpassword
TZ: America/New_York
volumes:
— postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:

These methods ensure your PostgreSQL container is running with the correct timezone.

Related Posts:

Episode # 0 Postgres and pgAdmin in Docker — Step-By-Step Guide For Setting Up and Connecting PostgreSQL and PGAdmin using Docker

Episode # 1Postgres and pgAdmin in Docker Compose — Setup Guide — PostgreSQL Series, Episode 1

Episode # 2 — Mastering PostgreSQL and pgAdmin Syntax — SQL Query Examples: Database Operations and Analysis in Action

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jungletronics
Jungletronics

Published in Jungletronics

Explore our insights on Django, Python, Rails, Ruby, and more. We share code, hacks, and academic notes for developers and tech enthusiasts. Happy reading!

J3
J3

Written by J3

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️

No responses yet

Write a response