Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore database from dump or sql file in docker using volume?

I am trying to run my database in docker which already have some data into it, But when I run it in docker it gives empty .. So how to restore my database in docker PostgreSQL image.

like image 453
Yesha Dave Avatar asked Oct 30 '19 10:10

Yesha Dave


People also ask

How do I back up Docker volumes?

To back up a data volume you can run a new container using the volume you want to back up and executing the tar command to produce an archive of the volume content as described in the docker user guide. In your particular case, the data volume is used to store the data for a MySQL server.


1 Answers

First, Start your docker posgtres container. Then execute the following command:

docker exec -i <CONTAINER> psql -U <USER> -d <DB-NAME> < <PATH-TO-DUMP>

  • <CONTAINER> : Container ID or container name
  • <USER> : User of PostgreSQL DBMS. If you use the standard postgres image with no specific config the user is postgres
  • <DB-NAME>: The name of the DB. This DB should already exist. You can create a db when running your postgres container by specifying an environment variable named POSTGRES_DB
  • <PATH-TO-DUMP>: Specify here the path to your dump file

Example: docker exec -i mypostgres psql -U postgres -d mydb < backup.sql

Hope this helps.

like image 87
ieggel Avatar answered Oct 13 '22 04:10

ieggel