简体   繁体   English

如何将 PostgreSQL 转储文件恢复到 Postgres 数据库中?

[英]How to restore PostgreSQL dump file into Postgres databases?

I have a dump file with a .SQL extension (in fact it is a plain-text SQL file).我有一个扩展名为.SQL的转储文件(实际上它是一个纯文本 SQL 文件)。 I want to restore it into my created databases.我想将它恢复到我创建的数据库中。 I am using pgAdmin III , and when I use its "Restore Wizard" it does not highlight the button "Restore".我正在使用pgAdmin III ,当我使用它的“恢复向导”时,它没有突出显示“恢复”按钮。 Instead it is expecting a .backup file extension.相反,它需要一个.backup文件扩展名。

I tried using shell the commands for restoring the dump, but it still didn't work.我尝试使用 shell 恢复转储的命令,但它仍然没有用。

I am a newbie at this.我是这方面的新手。 If anybody could help me I would be obliged.如果有人可以帮助我,我将有义务。

Edit编辑

I used following command to the Shell SQL Pane of PostGres while sitting at the newTestDB.坐在 newTestDB 上时,我对 PostGres 的 Shell SQL 窗格使用了以下命令。

newTestDB-# \i E:\db-rbl-restore-20120511_Dump-20120514.sql

It still gave the same error ("Permission Denied").它仍然给出相同的错误(“权限被拒绝”)。

After elevating permissions it just shows me the default tables of PostgreSQL:提升权限后,它只显示 PostgreSQL 的默认表:

      List of tablespaces
Name       |  Owner   | Location
-----------+----------+----------
pg_default | postgres |
pg_global  | postgres |

(2 rows)

I don't know what to do for importing/restoring database from an SQL file.我不知道如何从 SQL 文件导入/恢复数据库。

You didn't mention how your backup was made, so the generic answer is: Usually with the psql tool. 你没有提到你的备份是如何制作的,所以通用答案是: 通常使用psql工具。

Depending on what pg_dump was instructed to dump, the SQL file can have different sets of SQL commands. 根据指示转储的pg_dump ,SQL文件可以具有不同的SQL命令集。 For example, if you instruct pg_dump to dump a database using --clean and --schema-only , you can't expect to be able to restore the database from that dump as there will be no SQL commands for COPYing (or INSERTing if --inserts is used ) the actual data in the tables. 例如,如果指示pg_dump使用--clean--schema-only转储数据库,则不能期望能够从该转储恢复数据库,因为没有用于COPYing的SQL命令(或者如果INSITING则为--inserts使用插入)表中的实际数据。 A dump like that will contain only DDL SQL commands, and will be able to recreate the schema but not the actual data. 像这样的转储将只包含DDL SQL命令,并且能够重新创建模式而不是实际数据。

A typical SQL dump is restored with psql : 使用psql恢复典型的SQL转储:

psql (connection options here) database  < yourbackup.sql

or alternatively from a psql session, 或者从psql会话中,

psql (connection options here) database
database=# \i /path/to/yourbackup.sql

In the case of backups made with pg_dump -Fc ("custom format"), which is not a plain SQL file but a compressed file, you need to use the pg_restore tool. 如果使用pg_dump -Fc (“自定义格式”)进行备份(不是普通的SQL文件而是压缩文件),则需要使用pg_restore工具。

If you're working on a unix-like, try this: 如果您正在使用unix,请尝试以下方法:

man psql
man pg_dump
man pg_restore

otherwise, take a look at the html docs . 否则,看一下html文档 Good luck! 祝好运!

The problem with your attempt at the psql command line is the direction of the slashes: 尝试psql命令行的问题是斜杠的方向:

newTestDB-# /i E:\db-rbl-restore-20120511_Dump-20120514.sql   # incorrect
newTestDB-# \i E:/db-rbl-restore-20120511_Dump-20120514.sql   # correct

To be clear, psql commands start with a backslash, so you should have put \\i instead. 要清楚, psql命令以反斜杠开头,所以你应该放置\\i而不是。 What happened as a result of your typo is that psql ignored everything until finding the first \\ , which happened to be followed by db , and \\db happens to be the psql command for listing table spaces , hence why the output was a List of tablespaces . 由于你的拼写错误导致psql忽略了所有内容,直到找到第一个\\ ,后面跟着db ,并且\\db碰巧是用于列出表空间psql命令,因此为什么输出是表空间列表 It was not a listing of "default tables of PostgreSQL" as you said. 正如你所说,它不是“PostgreSQL默认表”的列表。

Further, it seems that psql expects the filepath argument to delimit directories using the forward slash regardless of OS (thus on Windows this would be counter-intuitive). 此外,似乎psql期望filepath参数使用正斜杠来分隔目录而不管操作系统(因此在Windows上这将是违反直觉的)。

It is worth noting that your attempt at "elevating permissions" had no relation to the outcome of the command you attempted to execute. 值得注意的是,您“提升权限”的尝试与您尝试执行的命令的结果无关。 Also, you did not say what caused the supposed "Permission Denied" error. 此外,您没有说出导致所谓的“权限被拒绝”错误的原因。

Finally, the extension on the dump file does not matter, in fact you don't even need an extension. 最后,转储文件的扩展名无关紧要,实际上你甚至不需要扩展名。 Indeed, pgAdmin suggests a .backup extension when selecting a backup filename, but you can actually make it whatever you want, again, including having no extension at all. 实际上, pgAdmin在选择备份文件名时建议使用.backup扩展名,但实际上你可以实现它想要的任何内容,包括根本没有扩展名。 The problem is that pgAdmin seems to only allow a "Restore" of "Custom or tar" or "Directory" dumps (at least this is the case in the MAC OS X version of the app), so just use the psql \\i command as shown above. 问题是pgAdmin似乎只允许“自定义或tar”或“目录”转储的“恢复”(至少在应用程序的MAC OS X版本中就是这种情况),所以只需使用psql \\i命令如上所示。

By using pg_restore command you can restore postgres database 通过使用pg_restore命令,您可以恢复postgres数据库

First open terminal type 首先打开终端类型

sudo su postgres

Create new database 创建新数据库

createdb [database name] -O [owner] createdb [数据库名称] -O [所有者]

createdb test_db [-O openerp]

pg_restore -d [Database Name] [path of dump file] pg_restore -d [数据库名称] [转储文件的路径]

pg_restore -d test_db /home/sagar/Download/sample_dbump

Wait for completion of database restoring. 等待数据库恢复完成。

Remember that dump file should have read, write, execute access, so for that you can apply chmod command 请记住,转储文件应具有读取,写入和执行访问权限,因此您可以应用chmod命令

1.open the terminal. 打开终端。

2.backup your database with following command 2.使用以下命令备份数据库

your postgres bin - /opt/PostgreSQL/9.1/bin/ 你的postgres bin - /opt/PostgreSQL/9.1/bin/

your source database server - 192.168.1.111 您的源数据库服务器 - 192.168.1.111

your backup file location and name - /home/dinesh/db/mydb.backup 您的备份文件位置和名称 - /home/dinesh/db/mydb.backup

your source db name - mydatabase 您的源数据库名称 - mydatabase

/opt/PostgreSQL/9.1/bin/pg_dump --host '192.168.1.111' --port 5432 --username "postgres" --no-password --format custom --blobs --file "/home/dinesh/db/mydb.backup" "mydatabase" /opt/PostgreSQL/9.1/bin/pg_dump --host'192.168.1.111' - port 5432 --username“postgres” - no-password --format custom --blobs --file“/ home / dinesh / db /mydb.backup“”mydatabase“

3.restore mydb.backup file into destination. 3.删除mydb.backup文件到目的地。

your destination server - localhost 您的目标服务器 - localhost

your destination database name - mydatabase 目标数据库名称 - mydatabase

create database for restore the backup. create database for restore backup。

/opt/PostgreSQL/9.1/bin/psql -h 'localhost' -p 5432 -U postgres -c "CREATE DATABASE mydatabase" /opt/PostgreSQL/9.1/bin/psql -h'localhost'-p 5432 -U postgres -c“CREATE DATABASE mydatabase”

restore the backup. 恢复备份。

/opt/PostgreSQL/9.1/bin/pg_restore --host 'localhost' --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup" /opt/PostgreSQL/9.1/bin/pg_restore --host'localhost' - port 5432 --username“postgres” - dbname“mydatabase” - no-password --clean“/ home / dinesh / db / mydb。备份”

Combining the advice from MartinP and user664833, I was also able to get it to work. 结合MartinP和user664833的建议,我也能够让它发挥作用。 Caveat is that entering psql from the pgAdmin GUI tool via choosing Plugins...PSQL Console sets the credentials and permission level for the psql session, so you must have Admin or CRUD permissions on the table and maybe also Admin on the DB (do not know for sure on that). 警告是通过选择插件从pgAdmin GUI工具输入psql ... PSQL控制台设置psql会话的凭据和权限级别,因此您必须对表具有管理员或CRUD权限,并且可能还有DB上的管理员(不要我当然知道)。 The command then in the psql console would take this form: 然后在psql控制台中的命令将采用以下形式:

postgres=# \i driveletter:/folder_path/backupfilename.backup

where postgres=# is the psql prompt, not part of the command. 其中postgres =#是psql提示符,不是命令的一部分。

The .backup file will include the commands used to create the table, so you may also get things like "ALTER TABLE ..." commands in the file that get executed but reported as errors. .backup文件将包含用于创建表的命令,因此您可能会在文件中获得“ALTER TABLE ...”命令,这些命令会被执行但报告为错误。 I suppose you can always delete these commands before running the restore but you're probably better safe than sorry to keep them in there, as these will not likely cause the restore of data to fail. 我想你总是可以在运行恢复之前删除这些命令,但是你可能比把握它们更安全,因为它们不会导致数据恢复失败。 But always check to be sure the data you wanted to resore actually got there. 但是要经常检查以确保您想要恢复的数据实际到达那里。 (Sorry if this seems like patronizing advice to anyone, but it's an oversight that can happen to anyone no matter how long they have been at this stuff -- a moment's distraction from a colleague, a phone call, etc., and it's easy to forget this step. I have done it myself using other databases earlier in my career and wondered "Gee, why am I not seeing any data back from this query?" Answer was the data never actually got restored, and I just wasted 2 hours trying to hunt down suspected possible bugs that didn't exist.) (对不起,如果这似乎是对任何人的光顾建议,但这是一个疏忽,任何人都可能发生这种疏忽,无论他们对这些东西有多长时间 - 一时间分散了同事,打电话等等,这很容易忘了这一步。我在职业生涯早期使用其他数据库自己完成了这项工作,并想知道“哎呀,为什么我从这个查询中看不到任何数据呢?”答案是数据从未实际恢复,我只是浪费了2个小时的尝试寻找可能存在的不存在的错误。)

您可能需要在数据库级别设置允许架构所有者还原转储的权限。

I find that psql.exe is quite picky with the slash direction, at least on windows (which the above looks like). 我发现psql.exe在斜线方向上非常挑剔,至少在windows上(上面看起来像)。

Here's an example. 这是一个例子。 In a cmd window: 在cmd窗口中:

C:\Program Files\PostgreSQL\9.2\bin>psql.exe -U postgres
psql (9.2.4)
Type "help" for help.

postgres=# \i c:\temp\try1.sql    
c:: Permission denied
postgres=# \i c:/temp/try1.sql
CREATE TABLE
postgres=#

You can see it fails when I use "normal" windows slashes in a \\i call. 当我在\\i调用中使用“普通”窗口斜杠时,您可以看到它失败。 However both slash styles work if you pass them as input params to psql.exe , for example: 但是如果将它们作为输入参数传递给psql.exe ,则两种斜杠样式 psql.exe ,例如:

C:\Program Files\PostgreSQL\9.2\bin>psql.exe -U postgres -f c:\TEMP\try1.sql
CREATE TABLE

C:\Program Files\PostgreSQL\9.2\bin>psql.exe -U postgres -f c:/TEMP/try1.sql
CREATE TABLE

C:\Program Files\PostgreSQL\9.2\bin>

This is what worked for me when restoring Postgres database in Windows. To begin with, right click the command shell and select "Run as administrator" to avoid permission denied issues.这就是在 Windows 中恢复 Postgres 数据库时对我有用的方法。首先,右键单击命令 shell 和 select“以管理员身份运行”以避免权限被拒绝的问题。 Execute the same command but with some changes:执行相同的命令但进行一些更改:

 newTestDB-# \i E:/db-rbl-restore-20120511_Dump-20120514.sql

If you notice, the command "newTestDB-# \i E:\db-rbl-restore-20120511_Dump-20120514.sql" has been changed to "newTestDB-# \i E:/db-rbl-restore-20120511_Dump-20120514.sql" with the / being used in the file path instead of the \ slash.如果您注意到,命令“newTestDB-# \i E:\db-rbl-restore-20120511_Dump-20120514.sql”已更改为“newTestDB-# \i E:/db-rbl-restore-20120511_Dump-20120514.sql”。 sql",在文件路径中使用 / 而不是 \ 斜杠。

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

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