SQL:将备份文件从复制格式转换为插入格式

发布于 2024-09-01 13:33:07 字数 287 浏览 13 评论 0原文

我有一个使用 PHPPgadmin 使用 Export > 制作的 PostgreSQL 备份复制(而不是复制> SQL,这实际上是我需要的)。

文件包含如下条目:

COPY tablename(id, field) FROM stdin;
...

如何将此文件转换为 SQL 格式?

INSERT INTO tablename...

我想使用 Pgadmin 通过执行 SQL 命令导入此文件。

I have a PostgreSQL backup made with PHPPgadmin using Export > Copy (instead Copy > SQL which is actually what I need).

File contains entries like this:

COPY tablename(id, field) FROM stdin;
...

How to convert this file to SQL format?

INSERT INTO tablename...

I want to use Pgadmin to to import this file using execute SQL command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青柠芒果 2024-09-08 13:33:07

我不知道可以将“COPY”转换为“INSERT”语句的方法或工具。

但是使用“pg_dump”命令行工具,您可以使用“INSERT”而不是“COPY”语句创建数据库转储文件。简单地使用“--column-inserts”参数(也许“--inserts”对你来说也可以)。

编辑:

您可以做的是:

  1. 在本地计算机上创建一个新的 postgres 数据库
  2. 将数据库转储文件导入新创建的数据库

    psql -U ; <数据库> <转储.sql
    
  3. 并使用该数据库中的“--column-inserts”创建 postgres 转储

    pg_dump --column-inserts -U ; <数据库> > dumpWithInserts.sql
    

I don't know a way or a tool which can convert "COPY" into "INSERT" statements.

But with the "pg_dump" command line tool you can create a DB dump file with "INSERT" instead of "COPY" statements. Simple use the "--column-inserts" argument (may be "--inserts" is also ok for you).

EDIT:

What you can do is:

  1. Create a new postgres database on your local machine
  2. Import your DB dump file into the new created database

    psql -U <dbuser> <database> < dump.sql
    
  3. And create a postgres dump with "--column-inserts" from that database

    pg_dump --column-inserts -U <dbuser> <database> > dumpWithInserts.sql
    
岁月静好 2024-09-08 13:33:07

我编写了一个工具来将复制转储转换为插入转储:

https://github.com/frddez /pg-dump2insert

例如,您可以使用它在另一个数据库中加载转储或搜索特定的已删除值。

I've wrote a tool to convert copy-from dump to inserts dump :

https://github.com/freddez/pg-dump2insert

You can use it to load a dump in another database or search for specific deleted values for example.

乱了心跳 2024-09-08 13:33:07

您无法将其转换为 SQL 格式(至少不能直接转换)。

如果您无法重新导出,那么更简单的选择是绕过phpPgadmin
登录您的服务器并运行类似的命令

cat [yourdump.sql]  | psql [your connections args]

如果您没有服务器的 shell 访问权限,您可以尝试上传文件(通过 SFTP 或 FTP)并通过 phpPgAdmin 使用 "COPY FROM < ;服务器上的文件路径>”。但是,如果有很多表,您必须(我相信)拆分文件并一次执行一个。

http://phppgadmin.sourceforge.net/?page=faq

You can't convert that to SQL format (at least not straightforwardly).

If you can't re export, then the simpler option is to bypass phpPgadmin,
login in your server and run something like

cat [yourdump.sql]  | psql [your connections args]

If you don't have shell access to your server, you might try you upload the file (via SFTP or FTP) and load it thorugh phpPgAdmin with "COPY <table> FROM <path_to_file_on_server>". But if there are many tables, you must (I believe) split the file and do it one at a time.

http://phppgadmin.sourceforge.net/?page=faq

寂寞花火° 2024-09-08 13:33:07

您可以尝试从 PostgreSQL 9.3 开始使用的 COPY FROM PROGRAM 语法,

例如:
从程序'echo“1 2 \N Ivan Ivanov 42 ...”复制tbl_customers(id,address_id,insurance_id,fullname,secret_code ...)'

You can try COPY FROM PROGRAM syntax, available since PostgreSQL 9.3

For example:
COPY tbl_customers (id, address_id, insurance_id, fullname, secret_code ...) FROM PROGRAM 'echo "1 2 \N Ivan Ivanov 42 ..."'

衣神在巴黎 2024-09-08 13:33:07

要添加 Steffen 答案,最好还添加 --rows-per-insert (添加到 v12 的 pg_dump 中)如果你想为所有值添加一个大插入:(

pg_dump --column-inserts --rows-per-insert=2147483647 \
        -U <dbuser> <database> > dumpWithOneInsert.sql

灵感来自 https://stackoverflow.com /a/60621069/19888529

To add on Steffen answer, it is nice to also add --rows-per-insert (added to pg_dump for v12) if you want to have one big insert for all your values :

pg_dump --column-inserts --rows-per-insert=2147483647 \
        -U <dbuser> <database> > dumpWithOneInsert.sql

(inspired by https://stackoverflow.com/a/60621069/19888529)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文