Run Postgres and pgWeb on Docker

Shambhu Tiwary

Create the Environment File

.env

POSTGRES_USER=your_postgres_user
POSTGRES_PASSWORD=your_postgres_password
POSTGRES_DB=your_postgres_db


Update the docker-compose.yml File



version: '3.8'
services:
    db:
        image: postgres:latest
        container_name: postgres_db
        env_file:
            - .env
        ports:
            - "5432:5432"
        volumes:
            - postgres_data:/var/lib/postgresql/data
    pgweb:
        image: sosedoff/pgweb
        container_name: pgweb
        depends_on:
             - db
        ports:
            - "8081:8081"
        environment:
            -DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
        env_file:
            - .env
volumes:
    postgres_data:

Steps to Run the Containers

  1. Create the Environment File: Create a file named .env and add the PostgreSQL environment variables as shown above.
  2. Create the Docker Compose File: Create a file named docker-compose.yml and add the content provided.
  3. Run Docker Compose: Open a terminal, navigate to the directory where these files are located, and run:
docker-compose up -d

Explanation

  • Environment File: Both PostgreSQL and PGWeb services use the same .env file.
  • DATABASE_URL: The DATABASE_URL in the PGWeb service is constructed using environment variables defined in the common .env file.
You can access PGWeb at http://localhost:8081 to manage your PostgreSQL database.







 

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.