Search code examples
postgresqldockercontainerspgadmin

Connecting pgAdmin and PostgreSQL to Docker: container shows an empty database


I need help with Connecting pgAdmin and Postgres to the Docker container. I run everything locally on my Mac, previously I had already installed Postgres there using homebrew, that’s why I use port 5431:5432. Following the video “Connecting pgAdmin and Postgres” at minute 6:09 I make these steps and get an empty database, though it was visible before all of these manipulations: After I run:

docker run -it \\
  -e PGADMIN_DEFAULT_EMAIL="[email protected]" \\
  -e PGADMIN_DEFAULT_PASSWORD="root" \\
  -p 8080:80 \\
  dpage/pgadmin4

I press control + C

  1. Then I stop the container with postgres (docker stop containerID)
  2. Creating a network
  3. Running this:
docker run -it \\
  -e POSTGRES_USER="root" \\
  -e POSTGRES_PASSWORD="root" \\
  -e POSTGRES_DB="ny_taxi" \\
  -v $(pwd)/ny_taxi_postgres_data:/var/lib/postgres/data \\
  -p 5431:5432 \\
  --network=pg-network \\
  --name pg-database \\
  postgres:13 
  1. Now when we are checking the data, I have an empty database.
root@localhost:ny_taxi> select count(1) from yellow_taxi_data;
relation "yellow_taxi_data" does not exist
LINE 1: select count(1) from yellow_taxi_data

When I press \d :

root@localhost:ny_taxi> \d
+--------+------+------+-------+
| **Schema** | **Name** | **Type** | **Owner** | |
--------+------+------+-------|
+--------+------+------+-------+

The database ny_taxi appears to be empty.

The list of all databases with command \l:

root@localhost:postgres> \l
+-----------+-------+----------+------------+------------+-------------------+
| Name      | Owner | Encoding | Collate    | Ctype      | Access privileges |
|-----------+-------+----------+------------+------------+-------------------|
| ny_taxi   | root  | UTF8     | en_US.utf8 | en_US.utf8 | <null>            |
| postgres  | root  | UTF8     | en_US.utf8 | en_US.utf8 | <null>            |
| template0 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root           |
|           |       |          |            |            | root=CTc/root     |
| template1 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root           |
|           |       |          |            |            | root=CTc/root     |
+-----------+-------+----------+------------+------------+-------------------+

What should I do? What’s the issue? Server? I am dealing with Docker for the first time. Your help will be very appreciated.


Solution

  • I see the volume is not added to the postgres container.

    The default postgresql data directory is /var/lib/postgresql/data not /var/lib/postgres/data so instead of:

    docker run -it \\
      -e POSTGRES_USER="root" \\
      -e POSTGRES_PASSWORD="root" \\
      -e POSTGRES_DB="ny_taxi" \\
      -v $(pwd)/ny_taxi_postgres_data:/var/lib/postgres/data \\
      -p 5431:5432 \\
      --network=pg-network \\
      --name pg-database \\
      postgres:13 
    
    

    Try

    docker run -it \\
      -e POSTGRES_USER="root" \\
      -e POSTGRES_PASSWORD="root" \\
      -e POSTGRES_DB="ny_taxi" \\
      -v $(pwd)/ny_taxi_postgres_data:/var/lib/postgresql/data \\
      -p 5431:5432 \\
      --network=pg-network \\
      --name pg-database \\
      postgres:13 
    

    To verify your postgres data directory, you can run SHOW data_directory; with superuser access or run this query select setting from pg_settings where name = 'data_directory';

    I hope this helps.