简体   繁体   中英

Unable to pg_restore SQL file on remote Linux VM

I am using PostgreSQL as my DB.

I have SCPed a .sql file on my remote Ubuntu VM.

I did sudo su - postgres and create a DB.

then I switch backed to my original account and tried this:

sudo -su postgres pg_restore < temp.sql

The command ran successfully.

But when I again switched back to postgres user and checked the db for the list of tables using \\dt I found no tables.

What am I doing wrong?

'pg_restore' is meant to restore files generated by 'pg_dump'.

From the man page

pg_restore is a utility for restoring a PostgreSQL database from an archive created by pg_dump(1) in one of the non-plain-text formats.

https://www.postgresql.org/docs/9.2/static/app-pgrestore.html

If your file was generated by pg_dump you probably need to at least tell it which database to dump into:

pg_restore -d my_new_database temp.sql

My own experience with pg_restore across different flavors shows that many times I need to specify the format of the dump file, even if it was in 'native' format, despite the manpage indicating that it would detect the format.

pg_restore -d my_new_database -Fc temp.dump

This is only a guess, but I'm thinking if the tables actually restored, without specifying the db, they got dumped into the default database. You can check this by listing the tables in the 'postgres' database (there should be none).

postgres=#\c postgres
You are now connected to database "postgres" as user "postgres".
postgres=#\dt
No relations found.

If your tables did restore into the default database they will be listed.

Plain text SQL files need to be treated differently, usually executed via SQL commands using psql.

psql -d my_database < temp.sql

Assuming you generated your database backup similar to this:

pg_dump -a --inserts databasename > exportfilename.sql

Try restoring your file like this:

psql databasename -f exportfilename.sql

Postgres pg_restore as mentioned above is only meant to be used with dump files, you can't restore it like that. Check for this answer in the official postgres.org website

Basically you don't use pg_restore with sql files ---> https://www.postgresql.org/message-id/AANLkTi%3DAqmWrUR4f8%2BEfCHzP%2BQrL1%3DunRLZp_jX7SoqF%40mail.gmail.com

I faced the same issue when running the psql command from within the terminal with ubuntu user. The command would end with an out of memory error.

I then switched to the postgres user with sudo -i -u postgres .

From there I simply run psql databasename -f databasefilebackup.sql , as mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM