To backup my "DVD_RENTAL_DB" database to a local folder on my computer I had to use the below in the Windows command prompt while running it as an administrator:
Don't use shell redirection (>) on Windows with pg_dump. The shell will helpfully "correct" encoding issues and corrupt your dump.
Instead, specify the output filename with the -f option (enter your respective information):
"C:\Program Files\PostgreSQL\14\bin\pg_dump" -U postgres -p 5432 -W -F p -h localhost -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql DVD_RENTAL_DB
This worked for me ONLY after I put double quotes around the pg_dump executable file path, before when I was adding the file path without double quotes the back up was not working; probably due to spaces in my file path. The PostgreSQL documentation didn't mention anything about double quotes around the pg_dump executable file path.
To Restore my Database I used the following in the Windows command prompt while running it as an administrator:
- Open the Windows Command Prompt as an Administrator and you should be in this directory:
C:\Windows\System32>
- Then type the following:
cd C:\Program Files\PostgreSQL\14\bin\
- Then you'll be here in this directory:
C:\Program Files\PostgreSQL\14\bin>
- Type the following (enter your respective information):
psql -U postgres -d DVD_RENTAL_DB -f C:\Postgres_DB_Backups\DVD_RENTAL_DB.sql
- You'll be prompted for your password, then your database will be restored.