16

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.

Yesha Dave
  • 171
  • 1
  • 1
  • 7
  • Can you be a bit more clear on how you start your docker container? By default the postgresql container will store it's data in the container itself. if you delete it and restart the container, all data will be gone. Try looking into mounting a volume into a docker container: https://docs.docker.com/storage/volumes/ – koenge Oct 30 '19 at 10:33
  • I have dump as well as sql file of database, how can i restore in docker db.FYI, I am running docker on windows Machine. – Yesha Dave Oct 30 '19 at 10:47
  • Maybe also show the `docker run command` that you used to launch the container – ieggel Oct 30 '19 at 11:07

1 Answers1

37

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.

ieggel
  • 891
  • 6
  • 12
  • 3
    I am getting this error:-OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"-U\": executable file not found in $PATH": unknown – Yesha Dave Oct 30 '19 at 11:18
  • Please give more detail in your original post. Tell us all the steps you took right from the beginning. It is very hard to help with so little information. – ieggel Oct 30 '19 at 11:29
  • 1
    Hey... Thank you so much. Your command worked for me. It was my typo mistake. Silly me. Thanks a lot. – Yesha Dave Oct 31 '19 at 04:27
  • 2
    @ieggel The first line is missing the `psql` command between `` and `-U`, that's why it failed for Yesha Dave – hob Nov 29 '21 at 02:46