DEV Community

Tushar Patil
Tushar Patil

Posted on

🚀 Set Up PostgreSQL with pgAdmin Using Docker (Step-by-Step Guide)

🚀 Introduction

Managing PostgreSQL databases becomes much easier with pgAdmin, a powerful browser-based GUI. In this guide, you'll learn how to spin up both PostgreSQL and pgAdmin using Docker — no manual installation required!

📝 Note
Setting up PostgreSQL and pgAdmin using individual docker run commands is a valid approach, but it requires running multiple commands manually.

If you prefer using individual Docker commands, follow the steps below.
If you want a cleaner setup, skip directly to the Docker Compose section and run everything with a single command.


🛠️ Prerequisites

Make sure the following are installed:


Step 1: Pull PostgreSQL Docker Image

docker pull postgres
Enter fullscreen mode Exit fullscreen mode

Step 2: Run PostgreSQL Container

docker run --name my-postgres -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=mydb -p 5432:5432 -d postgres
Enter fullscreen mode Exit fullscreen mode

🔍 This sets up a PostgreSQL container with:

  • Username: admin
  • Password: secret
  • Database name: mydb
  • Port: Exposes DB on localhost:5432

Step 3: Pull pgAdmin Docker Image

docker pull dpage/pgadmin4
Enter fullscreen mode Exit fullscreen mode

Step 4: Run pgAdmin container

docker run --name pgadmin-container \
  -e [email protected] \
  -e PGADMIN_DEFAULT_PASSWORD=admin123 \
  -p 5050:80 \
  -d dpage/pgadmin4
Enter fullscreen mode Exit fullscreen mode

-Email: [email protected]
-Password: admin123
-port : -p 5050
-image name(you also can use image id) : -d dpage/pgadmin4

This Starts pgAdmin on http://localhost:5050


Step 5: Access pgAdmin in Browser

Open your browser and go to:

http://localhost:5050
Enter fullscreen mode Exit fullscreen mode

step 6: Add a New Server in pgAdmin

After logging into pgAdmin:

Click "Query Tool Workspace" (tool-> query tool) in the dashboard.
Under the General tab, fill the following information

  • server name: postgres
  • Port: 5432 (port you mentioned while running postgres)
  • Host Name: my-postgres (your postgres container name or service name)
  • Username: admin (mentioned while running postgres container)
  • Password: secret (your postgres password here)
  • Database: mydb (database name)

Image description


Step 7: run sample query

to ensure all is working properly run the following queries in pgAdmin's query space

1.Create Table Users

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL, 
    password_hash VARCHAR(255) NOT NULL
);
Enter fullscreen mode Exit fullscreen mode

2.Insert a user in users Table

INSERT INTO users (username, password_hash) VALUES ('your_username', 'your_hashed_password');
Enter fullscreen mode Exit fullscreen mode

3.query all users from database

SELECT * FROM users;
Enter fullscreen mode Exit fullscreen mode

🚀 Using Docker-compose

1.Create docker-compose.yaml file

docker-compose.yaml

version: '3.8'

services:
  postgres:
    image: postgres:15
    container_name: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres123
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin123
    ports:
      - "5050:80"
    depends_on:
      - postgres
    volumes:
      - pgadmin_data:/var/lib/pgadmin

volumes:
  pgdata:
  pgadmin_data:
Enter fullscreen mode Exit fullscreen mode

2.Run it

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

3. Access pgAdmin on Browser

open browser and enter url http://localhost:5050/browser
now follow the process from Step 5 mentioned above

Top comments (0)