How to generate a Postgresql Dump from a Docker container?

前端 未结 5 596
忘掉有多难
忘掉有多难 2021-01-31 02:55

I would like to have a way to enter into the Postgresql container and get a data dump from it.

相关标签:
5条回答
  • 2021-01-31 03:26

    Use the following command from a UNIX terminal:

    docker exec <container_name> pg_dump <schema_name> > backup
    

    The following command will dump only inserts from all tables:

    docker exec <container_name> pg_dump --column-inserts --data-only  <schema_name> > inserts.sql
    
    0 讨论(0)
  • 2021-01-31 03:27

    To run the container that has the Postgres user and password, you need to have preconfigured variables as container environment variable. For example:

    docker run -it --rm --link <container_name>:<data_container_name> -e POSTGRES_PASSWORD=<password> postgres /usr/bin/pg_dump -h <data_container_name> -d <database_name> -U <postgres_username> > dump.sql

    0 讨论(0)
  • 2021-01-31 03:37

    Another workaround method is to start postgre sql with a mountpoint to the location of the dump in docker.

    like docker run -v <location of the files>. Then perform a docker inspect on the docker running container

    docker inspect <image_id>
    

    you can find "Volumes" tag inside and a corresponding location.Go to the location and you can find all the postgresql/mysql files.It worked for me.Let us know if that worked for you also.

    Good luck

    0 讨论(0)
  • 2021-01-31 03:40

    Although the mountpoint solution above looked promising, the following is the only solution that worked for me after multiple iterations:

    docker run -it  -e PGPASSWORD=my_password postgres:alpine  pg_dump  -h hostname -U my_user my_db > backup.sql
    

    What was unique in my case: I have a password on the database that needs to be passed in; needed to pass in the tag (alpine); and finally the hosts version of the psql tools were different to the docker versions.

    0 讨论(0)
  • 2021-01-31 03:43

    I have container named postgres with mounted volume -v /backups:/backups

    To backup gziped DB my_db I use:

    docker exec postgres pg_dump -U postgres -F t my_db | gzip >/backups/my_db-$(date +%Y-%m-%d).tar.gz
    

    Now I have

    user@my-server:/backups$ ls
    my_db-2016-11-30.tar.gz
    
    0 讨论(0)
提交回复
热议问题