4

I am in the process of migrating all the database records from my docker Postgres environment. I am new to docker and volumes. I have a backup SQL file of Postgres in my local system. How do I access it in my Pgadmin that is running in docker to populate my database in the Postgres container of docker?

pgadmin that is running in docker

backup file that has data to populate by datbase in my personal device

This is my docker-compose file, is it possible to add a volume to map to my local device so that I can access files from my pc storage from pgadmin of the docker container?

version: "3.8"

services:
  postgres:
    image: postgres:11.5
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=****
      - POSTGRES_DB=nanomedicine_db
    ports:  #outside:inside(container)
      - 5432:5432
    networks:
      - shared-network
    volumes:
      - C:/profitional/nanomedicine/data:/var/lib/postgresql/data

  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: ****
    ports:
      - 5050:80
    networks:
      - shared-network
    depends_on:
      - postgres
    
  server:
    container_name: nanomedicine_server
    restart: unless-stopped
    image: nanomedicine-server-image:1.0.0
    build:
      context: nanomedicine-backend 
      target: production
      dockerfile: Dockerfile
    ports:  #outside:inside(container)
      - 8080:8080
    networks:
      - shared-network
    depends_on:
      - postgres

  client:
    container_name: nanomedicine_client
    restart: unless-stopped
    image: nanomedicine-client-image:1.0.0
    build:
      context: nanomedicine-frontend 
      target: production
      dockerfile: Dockerfile
    ports:  #outside:inside(container)
      - 3000:3000

    networks:
      - shared-network
    depends_on:
      - server
volumes:
  postgres-volume:
    external: true
networks:
  shared-network:

To know a process of accesing my local files from docker container environment of pgadmin

2 Answers 2

4

is it possible to add a volume to map to my local device so that I can access files from my pc storage from pgadmin of the docker container?

Yes, that is exactly what you need to do!, and that technique is known as bind mount.

Note that there is an important difference between accessing a file from a SQL statement (using the pgAdmin editor) and from the pgAdmin GUI (for example, with the file selection utilities): if you are going to access the file from a SQL statement, you will have to perform the bind mount in the PostgreSQL container (since the SQL statement will be executed there), on the other hand, if you are going to access the file from the pgAdmin GUI, you will have to perform the bind mount in the pgAdmin container.

To perform the bind mount on any of the containers simply add a volume as follows:

volumes:
    - C:/path/to/local/directory:/path/to/container/directory

Note: make sure that /path/to/container/directory doesn't already exist in the container to avoid errors.

If you put the volume in the PostgreSQL container, you can already access the file from SQL statements (you can write them in the pgAdmin editor) using the path /path/to/container/directory/<filename>.

Finally, if you want to use pgAdmin's "Restore..." utility (I will leave images below), it is much easier for you to load the file from the GUI than to mount the volume in the pgAdmin container, as you will additionally have to set the PGADMIN_CONFIG_STORAGE_DIR environment variable in the pgAdmin container to be able to access your local files from the GUI, which is not trivial because of the operations that pgAdmin performs in its configuration.

Sign up to request clarification or add additional context in comments.

3 Comments

The restoration of the database is exactly what I was searching for, I couldn't find the place where to upload the files from my local system to the docker environment. Thank you so much. I have one more question. Is there any way that we can directly seed the .sql file to the Postgres server as soon as we run the docker-compose up(without manually restoring data in Pgadmin or typing commands to insert data after the docker container is running).
If that is not possible are there any commands that I can include with docker files so that the database will get populated as soon as the command is executed?
If I remember correctly, there is a way to give a SQL file to PostgreSQL so that it populates a database automatically, additionally, something you could also do is to create a new service in your Docker Compose to execute the commands to populate the database after the PostgreSQL service has finished its execution. Obviously, the comment box is insufficient to explain all that, so if you want to write a question (although I think there must already be one for all this), make me know and I'll try to answer it later.
2

The dpage/pgadmin4 image stores backups in /var/lib/pgadmin/storage/postgres_postgres.com directory, so you just need to add volume to your docker-compose.yml:

    volumes:
      - ./pgadmin:/var/lib/pgadmin/storage/postgres_postgres.com

However i prefer to run pgadmin in non-server mode, then it doesn't ask for login and have access to /home/pgadmin directory in container. That is my pgadmin config:

  pgadmin:
    container_name: xconsole2-backend-pgadmin
    image: dpage/pgadmin4
    volumes:
      - ./pgadmin:/home/pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: postgres
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
    restart: unless-stopped
    links:
      - postgres
    ports:
      - 8888:80
    networks:
      - default
    depends_on:
      postgres:
        condition: service_healthy

That way you will be able to see backups from ./pgadmin in /home/pgadmin in pgadmin4 list of backups from /home/pgadmin dir

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.