# ๐Ÿฆท DentalApp Database Restore Guide This document explains how to **restore the DentalApp PostgreSQL database** on a new PC using the latest backup file from the main PC. --- ## ๐Ÿงฉ Overview You will: 1. Create a PostgreSQL backup on the **main PC** 2. Copy it to the **target PC** 3. Restore it cleanly using `pg_restore` --- ## โš™๏ธ Prerequisites Before starting: - PostgreSQL is installed on both machines (same major version recommended) - The app is **not running** during restore - You know the database credentials _(from `.env` or environment variables)_ Example: ```bash DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp ``` ## ๐Ÿงญ Steps to Restore Database on Another PC ### ๐Ÿ–ฅ๏ธ Step 1 โ€” Create Backup on Main PC - Generate the backup.dump file from the backup page from the app. ### Step 2 โ€” Copy Backup File to Target PC - Transfer the backup file to the second PC using USB, network share, cloud, or SCP. = Example destination path: C:\db_backups\backup.dump ### ๐Ÿงน Step 3 โ€” Prepare the Target PC - Stop the DentalApp application to avoid database locks. - Ensure PostgreSQL is installed and running. - (Optional but recommended) Drop the existing database: ``` PGPASSWORD='mypassword' dropdb -U postgres -h localhost dentalapp ``` ### โ™ป๏ธ Step 4 โ€” Restore the Database # Case1: when we got a zip folder. -linux bash # 4.1) unzip to a directory ``` unzip backup.zip -d /tmp/dental_dump_dir ``` # 4.2) restore into an already-created DB named 'dentalapp' ``` PGPASSWORD='mypassword' createdb -U postgres -h localhost -O postgres dentalapp # optional PGPASSWORD='mypassword' pg_restore -U postgres -h localhost -d dentalapp -j 4 /tmp/dental_dump_dir or PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h localhost -C -d postgres /tmp/dental_dump_dir ``` # Case2: when we got a tar folder. -linux bash # 4.1) unzip to a directory ``` # create target dir and extract mkdir -p /tmp/dental_dump_dir tar -xzf backup.tar.gz -C /tmp/dental_dump_dir ``` # 4.2) restore into an already-created DB named 'dentalapp' ``` PGPASSWORD='mypassword' createdb -U postgres -h localhost -O postgres dentalapp # optional PGPASSWORD='mypassword' pg_restore -U postgres -h localhost -d dentalapp -j 4 /tmp/dental_dump_dir or PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h localhost -C -d postgres /tmp/dental_dump_dir ``` ### โœ… Step 5 โ€” Verify the Restore - Check that the tables are restored successfully: ``` PGPASSWORD='mypassword' psql -U postgres -h localhost -d dentalapp -c "\dt" ``` - You should see all the application tables listed. ### ๐Ÿงฉ Step 6 โ€” Update App Configuration - Ensure the .env file on the target PC points to the correct database: ``` DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp ``` - Then start the DentalApp application and verify that it connects and displays data correctly. # ๐Ÿง  Step 7 โ€” Tips - Use the same PostgreSQL version as the main PC. - For large databases, use parallel restore for speed: ``` pg_restore -U postgres -j 4 -d dentalapp backup.dump ``` - Always keep at least one recent backup archived safely. # If such error came: - pg_restore: error: unsupported version (1.16) in file header - use cmd: - 1. Add PGDG (official PostgreSQL) APT repo and its key, then update and install client-17 ``` sudo apt update && sudo apt install -y wget ca-certificates gnupg lsb-release curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/pgdg.gpg echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list sudo apt update sudo apt install -y postgresql-client-17 ``` - 2. Run pg_restore from the installed v17 binary (replace password as needed) ``` PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h localhost -C -d postgres ./backup.dump ```