简体   繁体   English

无法在远程 Linux VM 上 pg_restore SQL 文件

[英]Unable to pg_restore SQL file on remote Linux VM

I am using PostgreSQL as my DB.我使用 PostgreSQL 作为我的数据库。

I have SCPed a .sql file on my remote Ubuntu VM.我在远程 Ubuntu 虚拟机上SCP了一个.sql文件。

I did sudo su - postgres and create a DB.我做了sudo su - postgres并创建了一个数据库。

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.但是当我再次切换回postgres用户并使用\\dt检查表列表的数据库时,我发现没有表。

What am I doing wrong?我做错了什么?

'pg_restore' is meant to restore files generated by 'pg_dump'. 'pg_restore' 用于恢复由 '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. pg_restore 是一个实用程序,用于从 pg_dump(1) 创建的非纯文本格式之一的存档中恢复 PostgreSQL 数据库。

https://www.postgresql.org/docs/9.2/static/app-pgrestore.html 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_dump 生成的,您可能至少需要告诉它要转储到哪个数据库:

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 的经验表明,很多时候我需要指定转储文件的格式,即使它是“本机”格式,尽管联机帮助页表明它会检测格式。

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”数据库中的表(应该没有)来检查这一点。

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.纯文本 SQL 文件需要区别对待,通常使用 psql 通过 SQL 命令执行。

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.上面提到的 Postgres pg_restore仅用于dump文件,您不能像那样恢复它。 Check for this answer in the official postgres.org website在官方 postgres.org 网站上查看此答案

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基本上你使用带有sql文件的pg_restore ---> 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.使用ubuntu用户从终端内运行psql命令时,我遇到了同样的问题。 The command would end with an out of memory error.该命令将以out of memory不足错误结束。

I then switched to the postgres user with sudo -i -u postgres .然后我用sudo -i -u postgres切换到 postgres 用户。

From there I simply run psql databasename -f databasefilebackup.sql , as mentioned above.从那里我只需运行psql databasename -f databasefilebackup.sql ,如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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