Import SQL dump into PostgreSQL database

前端 未结 15 1607
我寻月下人不归
我寻月下人不归 2020-12-04 04:23

We are switching hosts and the old one provided a SQL dump of the PostgreSQL database of our site.

Now, I\'m trying to set this up on a local WAMP server to test thi

相关标签:
15条回答
  • 2020-12-04 05:00

    Works pretty well, in command line, all arguments are required, -W is for password

    psql -h localhost -U user -W -d database_name -f path/to/file.sql
    
    0 讨论(0)
  • 2020-12-04 05:02

    I believe that you want to run in psql:

    \i C:/database/db-backup.sql
    
    0 讨论(0)
  • 2020-12-04 05:02

    I tried many different solutions for restoring my postgres backup. I ran into permission denied problems on MacOS, no solutions seemed to work.

    Here's how I got it to work:

    Postgres comes with Pgadmin4. If you use macOS you can press CMD+SPACE and type pgadmin4 to run it. This will open up a browser tab in chrome.

    If you run into errors getting pgadmin4 to work, try killall pgAdmin4 in your terminal, then try again.


    Steps to getting pgadmin4 + backup/restore

    1. Create the backup

    Do this by rightclicking the database -> "backup"

    2. Give the file a name.

    Like test12345. Click backup. This creates a binary file dump, it's not in a .sql format

    3. See where it downloaded

    There should be a popup at the bottomright of your screen. Click the "more details" page to see where your backup downloaded to

    4. Find the location of downloaded file

    In this case, it's /users/vincenttang

    5. Restore the backup from pgadmin

    Assuming you did steps 1 to 4 correctly, you'll have a restore binary file. There might come a time your coworker wants to use your restore file on their local machine. Have said person go to pgadmin and restore

    Do this by rightclicking the database -> "restore"

    6. Select file finder

    Make sure to select the file location manually, DO NOT drag and drop a file onto the uploader fields in pgadmin. Because you will run into error permissions. Instead, find the file you just created:

    7. Find said file

    You might have to change the filter at bottomright to "All files". Find the file thereafter, from step 4. Now hit the bottomright "Select" button to confirm

    8. Restore said file

    You'll see this page again, with the location of the file selected. Go ahead and restore it

    9. Success

    If all is good, the bottom right should popup an indicator showing a successful restore. You can navigate over to your tables to see if the data has been restored propery on each table.

    10. If it wasn't successful:

    Should step 9 fail, try deleting your old public schema on your database. Go to "Query Tool"

    Execute this code block:

    DROP SCHEMA public CASCADE; CREATE SCHEMA public;
    

    Now try steps 5 to 9 again, it should work out

    Summary

    This is how I had to backup/restore my backup on Postgres, when I had error permission issues and could not log in as a superuser. Or set credentials for read/write using chmod for folders. This workflow works for a binary file dump default of "Custom" from pgadmin. I assume .sql is the same way, but I have not yet tested that

    0 讨论(0)
  • 2020-12-04 05:07

    make sure the database you want to import to is created, then you can import the dump with

    sudo -u postgres -i psql testdatabase < db-structure.sql
    

    If you want to overwrite the whole database, first drop the database

    # be sure you drop the right database !!!
    #sudo -u postgres -i psql -c "drop database testdatabase;"
    

    and then recreate it with

    sudo -u postgres -i psql -c "create database testdatabase;"
    
    0 讨论(0)
  • 2020-12-04 05:10

    Here is the command you are looking for.

    psql -h hostname -d databasename -U username -f file.sql
    
    0 讨论(0)
  • 2020-12-04 05:13

    I'm not sure if this works for the OP's situation, but I found that running the following command in the interactive console was the most flexible solution for me:

    \i 'path/to/file.sql'
    

    Just make sure you're already connected to the correct database. This command executes all of the SQL commands in the specified file.

    0 讨论(0)
提交回复
热议问题