ChatGPT解决这个技术问题 Extra ChatGPT

Import SQL dump into PostgreSQL database

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 this.

The only problem is that I don't have an idea how to import this database in the PostgreSQL 9 that I have set up.

I tried pgAdmin III but I can't seem to find an 'import' function. So I just opened the SQL editor and pasted the contents of the dump there and executed it, it creates the tables but it keeps giving me errors when it tries to put the data in it.

ERROR:  syntax error at or near "t"
LINE 474: t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view...

The lines:
COPY tb_abilities (active, creation, modtime, id, lang, title, description) FROM stdin;
t   2011-05-24 16:45:01.768633  2011-05-24 16:45:01.768633  view    nl ...  

I've also tried to do this with the command prompt but I can't find the command that I need.

If I do

psql mydatabase < C:/database/db-backup.sql;

I get the error

ERROR:  syntax error at or near "psql"
LINE 1: psql mydatabase < C:/database/db-backu...
        ^

What's the best way to import the database?

I had a similar error to your first one: ERROR: syntax error at or near "t". It turned out that I had only imported a partial schema and thus a CREATE TABLE statement earlier in the script had failed. Look through the full output of the import to find it.

M
Matthias Braun
psql databasename < data_base_dump

That's the command you are looking for.

Beware: databasename must be created before importing. Have a look at the PostgreSQL Docs Chapter 23. Backup and Restore.


I've tried that psql mydatabase < C:\database\db-backup.sql but i get the error Invalid command \database. I also tried with " " around it.
@Dazz You have to do this command from your command prompt (Start -> Run -> cmd) , not from the postgres prompt.
if i run it from cmd i get 'The '<' operator is reserved for future use.'
@Dazz:You could use -f switch (or --file) too
psql --username=postgres databasename < data_base_dump.sql
G
Gabriel Ramirez

Here is the command you are looking for.

psql -h hostname -d databasename -U username -f file.sql

Good, but better append also the "-L logfile.log" param to log the output on file.
getting this "The input is a PostgreSQL custom-format dump. Use the pg_restore command-line client to restore this dump to a database."
You can also: pg_restore -h hostname -d dbname -U username filename.sql
How to deal with ERROR: duplicate key value violates unique constraint? I would like psql to ignore when there's a duplicate key
In my case I am running postgres on different port, so I have to add -p along with the above command
j
j0k

I believe that you want to run in psql:

\i C:/database/db-backup.sql

Had to use this because Emacs' eshell doesn't support input redirection. Thanks.
Be sure to change to the database first with \c
use \ir if you want to give it a relative path.
I don't know why but I had to add quotes it worked. test_db=# \i 'C:/Users/paude/Downloads/event_logic.dump'
k
kenorb

That worked for me:

sudo -u postgres psql db_name < 'file_path'

r
rockusbacchus

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.


This solution worked for me while the most voted solution yielded "stdin is not a tty" error.
Just what I was looking for - a way to execute the script from inside the psql command line. Thanks!
Just wanted to thank you for this one. Should be included in the right answer.
this is correct, do not use back-slashes in the path in windows!
also for Windows, you need double-forward slashes
F
Feuda

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

After several tries this solution worked for me, pointing all the information necessary to execute the job: where to, database name, user, password and of course the file.
N
Nemo

Just for funsies, if your dump is compressed you can do something like

gunzip -c filename.gz | psql dbname

As Jacob mentioned, the PostgreSQL docs describe all this quite well.


V
Vincent Tang

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"

https://i.stack.imgur.com/2hQt9.png

2. Give the file a name.

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

https://i.stack.imgur.com/FyEZm.png

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

https://i.stack.imgur.com/5cXqM.png

4. Find the location of downloaded file

In this case, it's /users/vincenttang

https://i.stack.imgur.com/vJiyy.png

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"

https://i.stack.imgur.com/bAUhb.png

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:

https://i.stack.imgur.com/0FL3F.png

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

https://i.stack.imgur.com/woFcW.png

8. Restore said file

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

https://i.stack.imgur.com/i7cTv.png

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"

https://i.stack.imgur.com/bqmyP.png

Execute this code block:

DROP SCHEMA public CASCADE; CREATE SCHEMA public;

https://i.stack.imgur.com/BaHyr.png

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


r
rubo77

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;"

A
AConsumer

Follow the steps:

Go to the psql shell \c db_name \i path_of_dump [eg:-C:/db_name.pgsql]


V
Vajira Lasantha

I use:

cat /home/path/to/dump/file | psql -h localhost -U <user_name> -d <db_name>

Hope this will help someone.


d
davidauza.engineer

If you are using a file with .dump extension use:

pg_restore -h hostname -d dbname -U username filename.dump


K
Kiryl Plyashkevich

I noticed that many examples are overcomplicated for localhost where just postgres user without password exist in many cases:

psql -d db_name -f dump.sql

V
Vanessa MacDougal

You can do it in pgadmin3. Drop the schema(s) that your dump contains. Then right-click on the database and choose Restore. Then you can browse for the dump file.


But the 'restore' button is not click-able even after selecting the .sql file. This software seems to want some other kind of file - one with *.backup format. Clicking 'help' on that import-box refers to pg_restore - "...a utility for restoring a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats." The sql file the OP refers to is a plain-text format.
b
burney

Postgresql12

from sql file: pg_restore -d database < file.sql

from custom format file: pg_restore -Fc database < file.dump


M
Mohd Tauovir Khan

I had more than 100MB data, therefore I could not restore database using Pgadmin4.

I used simply postgres client, and write below command.

postgres@khan:/$ pg_restore -d database_name /home/khan/Downloads/dump.sql

It worked fine and took few seconds.You can see below link for more information. https://www.postgresql.org/docs/8.1/app-pgrestore.html