简体   繁体   English

将 SQL 转储导入 PostgreSQL 数据库

[英]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.我们正在切换主机,旧主机提供了我们站点的 PostgreSQL 数据库的 SQL 转储。

Now, I'm trying to set this up on a local WAMP server to test this.现在,我正在尝试在本地 WAMP 服务器上进行设置以进行测试。

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.唯一的问题是我不知道如何在我设置的 PostgreSQL 9 中导入这个数据库。

I tried pgAdmin III but I can't seem to find an 'import' function.我尝试了 pgAdmin III,但似乎找不到“导入”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.所以我刚刚打开 SQL 编辑器并将转储的内容粘贴到那里并执行它,它创建了表,但是当它尝试将数据放入其中时它一直给我错误。

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?导入数据库的最佳方法是什么?

psql databasename < data_base_dump

That's the command you are looking for.这就是您正在寻找的命令。

Beware: databasename must be created before importing.注意:必须在导入之前创建databasename Have a look at the PostgreSQL Docs Chapter 23. Backup and Restore .查看PostgreSQL Docs Chapter 23. Backup and Restore

这是您正在寻找的命令。

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

我相信你想在 psql 中运行:

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

这对我有用:

sudo -u postgres psql db_name < 'file_path'

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:我不确定这是否适用于 OP 的情况,但我发现在交互式控制台中运行以下命令对我来说是最灵活的解决方案:

\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.此命令执行指定文件中的所有 SQL 命令。

效果很好,在命令行中,所有参数都是必需的,-W 用于密码

psql -h localhost -U user -W -d database_name -f path/to/file.sql

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.正如 Jacob 所说, PostgreSQL 文档很好地描述了这一切。

I use:我用:

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

Hope this will help someone.希望这会帮助某人。

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

I tried many different solutions for restoring my postgres backup.我尝试了许多不同的解决方案来恢复我的 postgres 备份。 I ran into permission denied problems on MacOS, no solutions seemed to work.我在 MacOS 上遇到了权限被拒绝的问题,似乎没有任何解决方案有效。

Here's how I got it to work:这是我如何让它工作的:

Postgres comes with Pgadmin4. Postgres 带有 Pgadmin4。 If you use macOS you can press CMD + SPACE and type pgadmin4 to run it.如果你使用 macOS,你可以按CMD + SPACE并输入pgadmin4来运行它。 This will open up a browser tab in chrome.这将在 chrome 中打开一个浏览器选项卡。

If you run into errors getting pgadmin4 to work, try killall pgAdmin4 in your terminal, then try again.如果您在使 pgadmin4 工作时遇到错误, killall pgAdmin4在终端中尝试killall pgAdmin4 ,然后重试。


Steps to getting pgadmin4 + backup/restore获取 pgadmin4 + 备份/恢复的步骤

1. Create the backup 1. 创建备份

Do this by rightclicking the database -> "backup"通过右键单击数据库->“备份”来执行此操作

在此处输入图片说明

2. Give the file a name. 2. 为文件命名。

Like test12345 .test12345 Click backup.单击备份。 This creates a binary file dump, it's not in a .sql format这将创建一个二进制文件转储,它不是.sql格式

在此处输入图片说明

3. See where it downloaded 3.查看下载地址

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 4. 找到下载文件的位置

In this case, it's /users/vincenttang在这种情况下,它是/users/vincenttang

在此处输入图片说明

5. Restore the backup from pgadmin 5. 从 pgadmin 恢复备份

Assuming you did steps 1 to 4 correctly, you'll have a restore binary file.假设您正确地执行了步骤 1 到 4,您将拥有一个还原二进制文件。 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有人说有人去 pgadmin 并恢复

Do this by rightclicking the database -> "restore"通过右键单击数据库->“还原”来执行此操作

在此处输入图片说明

6. Select file finder 6.选择文件查找器

Make sure to select the file location manually, DO NOT drag and drop a file onto the uploader fields in pgadmin.确保手动选择文件位置,不要将文件拖放到 pgadmin 中的上传器字段上。 Because you will run into error permissions.因为你会遇到错误权限。 Instead, find the file you just created:相反,找到您刚刚创建的文件:

在此处输入图片说明

7. Find said file 7. 找到上述文件

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之后从第 4 步开始查找文件。现在点击右下角的“选择”按钮进行确认

在此处输入图片说明

8. Restore said file 8. 恢复上述文件

You'll see this page again, with the location of the file selected.您将再次看到此页面,并选择了文件的位置。 Go ahead and restore it继续并恢复它

在此处输入图片说明

9. Success 9. 成功

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: 10. 如果不成功:

Should step 9 fail, try deleting your old public schema on your database.如果第 9 步失败,请尝试删除数据库上的旧公共架构。 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现在再次尝试第 5 步到第 9 步,应该会成功

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.当我遇到错误权限问题并且无法以超级用户身份登录时,这就是我必须在 Postgres 上备份/恢复备份的方式。 Or set credentials for read/write using chmod for folders.或者使用chmod为文件夹设置读/写凭据。 This workflow works for a binary file dump default of "Custom" from pgadmin.此工作流程适用于 pgadmin 的“自定义”二进制文件转储默认值。 I assume .sql is the same way, but I have not yet tested that我假设.sql是相同的方式,但我还没有测试过

我注意到许多示例对于 localhost 来说过于复杂,其中在许多情况下只存在没有密码的 postgres 用户:

psql -d db_name -f dump.sql

You can do it in pgadmin3.您可以在 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.然后您可以浏览转储文件。

Follow the steps:按照步骤:

  1. Go psql shell去 psql 外壳
  2. \\c db_name
  3. \\i path_of_dump [eg:-C:/db_name.pgsql] \\i path_of_dump [eg:-C:/db_name.pgsql]

如果您使用带有.dump扩展名的文件, .dump使用:

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

您应该使用pg_restore

$ pg_restore -h hostname -d dbname -U username filename.sql

Postgresql12 PostgreSQL12

from sql file: pg_restore -d database < file.sql来自 sql 文件:pg_restore -d 数据库 <file.sql

from custom format file: pg_restore -Fc database < file.dump来自自定义格式文件:pg_restore -Fc 数据库 < file.dump

I had more than 100MB data, therefore I could not restore database using Pgadmin4.我有超过 100MB 的数据,因此我无法使用 Pgadmin4 恢复数据库。

I used simply postgres client, and write below command.我简单地使用了 postgres 客户端,并在下面写了命令。

postgres@khan:/$ pg_restore -d database_name /home/khan/Downloads/dump.sql 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 https://www.postgresql.org/docs/8.1/app-pgrestore.html

I used this我用这个

psql -d dbName -U username -f /home/sample.sql

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

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