Search code examples
postgresqldockerdocker-composedocker-volume

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.


Solution

  • 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.