SQL:将备份文件从复制格式转换为插入格式
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道可以将“COPY”转换为“INSERT”语句的方法或工具。
但是使用“pg_dump”命令行工具,您可以使用“INSERT”而不是“COPY”语句创建数据库转储文件。简单地使用“--column-inserts”参数(也许“--inserts”对你来说也可以)。
编辑:
您可以做的是:
将数据库转储文件导入新创建的数据库
并使用该数据库中的“--column-inserts”创建 postgres 转储
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:
Import your DB dump file into the new created database
And create a postgres dump with "--column-inserts" from that database
我编写了一个工具来将复制转储转换为插入转储:
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.
您无法将其转换为 SQL 格式(至少不能直接转换)。
如果您无法重新导出,那么更简单的选择是绕过phpPgadmin,
登录您的服务器并运行类似的命令
如果您没有服务器的 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
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
您可以尝试从 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 ..."'
要添加 Steffen 答案,最好还添加
--rows-per-insert
(添加到 v12 的 pg_dump 中)如果你想为所有值添加一个大插入:(灵感来自 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 :(inspired by https://stackoverflow.com/a/60621069/19888529)