How to Restore PostgreSQL from a .dmp File
It’s a fairly straightforward process, but if you’ve never done it, it can be a bit daunting. The first time I did it, I was lost and confused as well.
Step 1: Ensure pg_restore is installed
Make sure you have PostgreSQL installed. You can check by running:
[object Promise]If it’s not installed, install PostgreSQL first:
Ubuntu/Debian:
[object Promise]Mac (Homebrew):
[object Promise]Windows: Install PostgreSQL from postgresql.org.
Step 2: Navigate to the Dump File Location
Open a terminal and change to the directory where your .dmp file is stored (like /Downloads for example):
Step 3: Restore the Schema
Before restoring, ensure your target database exists. If not, create it:
[object Promise]Then restore the schema (without data). You want to restore the schema first because otherwise you’ll run into foreign key doesn’t exist errors.
You’ll be prompted to enter your database password. Make sure you set your host, port, username, and database name correctly, and reference the .dmp file properly:
Step 4: Restore the Data
Now restore the actual data:
[object Promise]Alternative: Full Restore in One Command
If you want to restore everything at once (schema + data):
[object Promise]Important Notes:
If the dump was created in plain SQL format (
[object Promise].sql), usepsqlinstead:If restoring to a fresh database, you don’t need
-cleanor-if-exists.If restoring from a remote dump, ensure network settings and firewall rules allow access.
Done!
Hope this was helpful :) Your PostgreSQL database should now be restored successfully. 🚀