Running PostgreSQL Docker Containers

1. Install Docker

2. Pull the PostgreSQL Docker Image

Open your terminal and run the following command to pull the latest PostgreSQL image:

docker pull postgres

3. Run a PostgreSQL Container

You can start a new PostgreSQL container with the following command:

docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
  • --name my_postgres: Names the container my_postgres.

  • -e POSTGRES_PASSWORD=mysecretpassword: Sets the password for the postgres user.

  • -d: Runs the container in detached mode.

  • -p 5432:5432: Maps port 5432 of the container to port 5432 on your host.

4. Accessing PostgreSQL

You can access PostgreSQL using a command-line tool like psql, or through GUI tools like pgAdmin.

Using psql:

You can access the PostgreSQL command line interface directly from your terminal:

docker exec -it my_postgres psql -U postgres

5. Create a Database

Once inside the psql shell, you can create a new database:

CREATE DATABASE mydatabase;

6. Using Docker Compose (Optional)

If you want to manage your PostgreSQL setup more easily, consider using Docker Compose. Create a docker-compose.yml file:

version: '3.8'
services:
  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: mydatabase
    ports:
      - "5432:5432"

Then run:

docker-compose up -d

7. Persisting Data

By default, data in a Docker container is ephemeral. To persist your PostgreSQL data, you can mount a volume:

docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data postgres

When you stop a Docker container that uses a volume for data storage, the data remains intact and can be accessed even after the container is stopped or removed. Here’s how you can access the storage again:

Steps to Access the Storage Again:

  1. Stop the Container (if not already stopped)
    If your container is still running, you can stop it with:

     docker stop my_postgres
    
  2. Restart the Container
    To access the data again, simply restart the container using the same volume:

     docker start my_postgres
    

    After restarting, you can access your PostgreSQL data as before.

  3. Accessing PostgreSQL
    Once the container is running again, you can connect to PostgreSQL using the psql command:

     docker exec -it my_postgres psql -U postgres
    

Important Notes

  • Volume Persistence: The -v pgdata:/var/lib/postgresql/data option ensures that the data is stored in a Docker volume named pgdata. This volume persists even if the container is removed.

  • Removing the Container: If you remove the container using docker rm my_postgres, the volume pgdata will still exist unless you explicitly remove it with:

      docker volume rm pgdata
    
  • Inspecting Volumes: You can check the existing volumes and their details using:

      docker volume ls
      docker volume inspect pgdata