Category: Databases

About databases

  • Restoring a SQL file to a dockerized Postgresql server

    Fotos grátis de Namibia
    If you are working with a dockerized Postgresql, perhaps, you need to restore a SQL file to a database. It is a simple task, so here are a few steps as a suggestion.

    Create a container

    docker run -e POSTGRES_PASSWORD=<your_passwd/> postgres

    Inspect the container

    docker inspect <container>

    In the out produced by docker inspect look for the informations about the container’s volume. Here is a example:

        {
            "Type": "volume",
            "Name": "your_container",
            "Source": "/var/lib/docker/volumes/your_container/_data",
            "Destination": "/data",
            "Driver": "local",
            "Mode": "rw",
            "RW": true,
            "Propagation": ""
        },

    Copy the SQL file

    Using the source property got above, copy the SQL file to the container:

    cp database_file.sql /var/lib/docker/volumes/<your_container>/_data

    Restore the database

    Now, in order to restore the database, you need to access the container’s shell:

    docker exec -it <container> bash

    Inside the container, you can use the appropriate tool to restore your database. Eg.: psql or pgrestore

    psql -U <user> -W -f <path_for_your_file/banco.sql> database

    That’s it! :-) Database restored.

    Looking for the SQL file

    If you get hard to find the SQL file, consider look for it at:

    /var/lib/postgresql/data

    References:

    More details at: https://simkimsia.com/how-to-restore-database-dumps-for-postgres-in-docker-container/