|
| 1 | +## Restoring the Database from SQL Dump |
| 2 | + |
| 3 | +We generally load a database backup from a JSON file by using the following command. |
| 4 | + |
| 5 | +``` |
| 6 | +docker-compose -f local.yml run --rm django python manage.py loaddata backup.json |
| 7 | +``` |
| 8 | + |
| 9 | +However, if the JSON file is particularly large (>1.5GB), Docker might struggle with this method. In such cases, you can use SQL dump and restore commands as an alternative. |
| 10 | + |
| 11 | +### Steps for Using SQL Dump and Restore |
| 12 | + |
| 13 | +1. Begin by starting only the PostgreSQL container. This prevents the Django container from making changes while the PostgreSQL container is starting up. |
| 14 | + |
| 15 | +``` |
| 16 | +docker-compose -f local.yml up postgres |
| 17 | +``` |
| 18 | + |
| 19 | +2. Find the container ID using `docker ps`, then enter the PostgreSQL container to execute commands. |
| 20 | + |
| 21 | +``` |
| 22 | +$ docker ps |
| 23 | +CONTAINER ID IMAGE COMMAND |
| 24 | +23d33f22cc43 sde_indexing_helper_production_postgres "docker-entrypoint.s…" |
| 25 | +
|
| 26 | +$ docker exec -it 23d33f22cc43 bash |
| 27 | +``` |
| 28 | + |
| 29 | +3. Create a connection to the database. |
| 30 | + |
| 31 | +``` |
| 32 | +psql -U <POSTGRES_USER> -d <POSTGRES_DB> |
| 33 | +``` |
| 34 | + |
| 35 | +**Note**: |
| 36 | +- For local deployment, refer to the `.envs/.local/.postgres` file for the `POSTGRES_USER` and `POSTGRES_DB` variables. |
| 37 | +- For production deployment, refer to the `.envs/.production/.postgres` file. |
| 38 | + |
| 39 | +4. Ensure that the database `<POSTGRES_DB>` is empty. Here's an example: |
| 40 | + |
| 41 | +``` |
| 42 | +sde_indexing_helper-# \c |
| 43 | +You are now connected to database "sde_indexing_helper" as user "VnUvMKBSdk...". |
| 44 | +sde_indexing_helper-# \dt |
| 45 | +Did not find any relations. |
| 46 | +``` |
| 47 | + |
| 48 | +If the database is not empty, delete its contents to create a fresh database: |
| 49 | + |
| 50 | +``` |
| 51 | +sde_indexing_helper=# \c postgres //connect to a different database before dropping |
| 52 | +You are now connected to database "postgres" as user "VnUvMKBSdk....". |
| 53 | +postgres=# DROP DATABASE sde_indexing_helper; |
| 54 | +DROP DATABASE |
| 55 | +postgres=# CREATE DATABASE sde_indexing_helper; |
| 56 | +CREATE DATABASE |
| 57 | +
|
| 58 | +``` |
| 59 | + |
| 60 | +5. Transfer the backup SQL dump (`backup.sql`) from your local machine to the PostgreSQL container. |
| 61 | + |
| 62 | +``` |
| 63 | +docker cp /local/path/backup.sql 23d33f22cc43:/ |
| 64 | +``` |
| 65 | + |
| 66 | +6. Import the SQL dump into the PostgreSQL container. |
| 67 | + |
| 68 | +``` |
| 69 | +psql -U <POSTGRES_USER> -d <POSTGRES_DB> -f backup.sql |
| 70 | +``` |
| 71 | + |
| 72 | +**Note**: To create a SQL dump of your PostgreSQL database, use the following command: |
| 73 | + |
| 74 | +``` |
| 75 | +pg_dump -U <POSTGRES_USER> -W -F p -f backup.sql <POSTGRES_DB> |
| 76 | +``` |
| 77 | + |
| 78 | +7. Bring up all containers at once, and create a superuser account for logging in. |
| 79 | + |
| 80 | +``` |
| 81 | +docker-compose -f local.yml up |
| 82 | +docker-compose -f local.yml run --rm django python manage.py createsuperuser |
| 83 | +``` |
| 84 | + |
| 85 | +8. Log in to the SDE Indexing Helper frontend to ensure that all data has been correctly populated in the UI. |
0 commit comments