使用 pg_dump 仅从数据库中的一张表获取插入语句

发布于 2024-09-01 15:17:57 字数 172 浏览 13 评论 0 原文

我正在寻找一种方法,使用 PostgreSQL 中的 pg_dump 从数据库中的一个特定表中获取所有行作为 INSERT 语句。

例如,我有表 A,表 AI 中的所有行都需要作为 INSERT 语句,它还应该将这些语句转储到文件中。

这可能吗?

I'm looking for a way to get all rows as INSERT statements from one specific table within a database using pg_dump in PostgreSQL.

E.g., I have table A and all rows in table A I need as INSERT statements, it should also dump those statements to a file.

Is this possible?

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

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

发布评论

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

评论(7

追风人 2024-09-08 15:17:57

如果版本< 8.4.0

pg_dump -D -t <table> <database>

如果您只需要 INSERT,而不需要 CREATE TABLE 等来首先设置表,请在 -t 之前添加 -a

版本 >= 8.4.0

pg_dump --column-inserts --data-only --table=<table> <database>

if version < 8.4.0

pg_dump -D -t <table> <database>

Add -a before the -t if you only want the INSERTs, without the CREATE TABLE etc to set up the table in the first place.

version >= 8.4.0

pg_dump --column-inserts --data-only --table=<table> <database>
英雄似剑 2024-09-08 15:17:57

如果您想将插入内容转储到 .sql 文件中:

  1. cd 到您希望 .sql 文件所在的位置
  2. 运行以下命令: pg_dump --column-inserts --data-only --table=; <数据库> > my_dump.sql

请注意,> my_dump.sql 此命令的一部分会将所有内容放入名为 my_dump 的 .sql 文件中

If you want to DUMP your inserts into an .sql file:

  1. cd to the location where you want the .sql file to be located
  2. Run the following command: pg_dump --column-inserts --data-only --table=<table> <database> > my_dump.sql

Note that the > my_dump.sql part of this command will put everything into an .sql file named my_dump

╄→承喏 2024-09-08 15:17:57

如果您使用远程访问并想要转储所有数据库数据,您可以使用:

pg_dump -a -h your_host -U your_user -W -Fc your_database > DATA.dump

它将创建包含所有数据库数据的转储并使用

pg_restore -a -h your_host -U your_user -W -Fc your_database DATA.dump

在数据库中插入相同的数据,考虑到您具有相同的结构

just in case you are using a remote access and want to dump all database data, you can use:

pg_dump -a -h your_host -U your_user -W -Fc your_database > DATA.dump

it will create a dump with all database data and use

pg_restore -a -h your_host -U your_user -W -Fc your_database < DATA.dump

to insert the same data in your data base considering you have the same structure

凡间太子 2024-09-08 15:17:57

放入我喜欢的脚本中:

#!/bin/bash
set -o xtrace # remove me after debug
TABLE=some_table_name
DB_NAME=prod_database

BASE_DIR=/var/backups/someDir
LOCATION="${BASE_DIR}/myApp_$(date +%Y%m%d_%H%M%S)"
FNAME="${LOCATION}_${DB_NAME}_${TABLE}.sql"

# Create backups directory if not exists
if [[ ! -e $BASE_DIR ]];then
       mkdir $BASE_DIR
       chown -R postgres:postgres $BASE_DIR
fi

sudo -H -u postgres pg_dump --column-inserts --data-only --table=$TABLE $DB_NAME > $FNAME
sudo gzip $FNAME

Put into a script I like something like that:

#!/bin/bash
set -o xtrace # remove me after debug
TABLE=some_table_name
DB_NAME=prod_database

BASE_DIR=/var/backups/someDir
LOCATION="${BASE_DIR}/myApp_$(date +%Y%m%d_%H%M%S)"
FNAME="${LOCATION}_${DB_NAME}_${TABLE}.sql"

# Create backups directory if not exists
if [[ ! -e $BASE_DIR ]];then
       mkdir $BASE_DIR
       chown -R postgres:postgres $BASE_DIR
fi

sudo -H -u postgres pg_dump --column-inserts --data-only --table=$TABLE $DB_NAME > $FNAME
sudo gzip $FNAME
空城缀染半城烟沙 2024-09-08 15:17:57

对于 postgres 12,这对我有用:

pg_dump -d <database> -t <table> > DATA.dump

for postgres 12, this worked for me:

pg_dump -d <database> -t <table> > DATA.dump
丿*梦醉红颜 2024-09-08 15:17:57

例如,您可以仅将用户(角色)johnapple数据库的数据导出到backup.sql,如下所示。 *我的回答解释了如何导出架构和数据:

pg_dump -U john -a apple > backup.sql

或者:

pg_dump -U john --data-only apple > backup.sql

并且,您可以仅使用 将用户(角色)johnapple 数据库的数据导出到 backup.sql INSERT 语句,其列名如下所示:

pg_dump -U john -a --column-inserts apple > backup.sql

并且,您只能将用户(角色)johnapple 数据库的数据导出到 backup.sql 仅包含 INSERT 语句,该语句没有列名,如下所示:

pg_dump -U john -a --inserts apple > backup.sql

并且,您可以仅导出特定表 person 的数据和用户(角色)johnapple 数据库的animalbackup.sql,只需INSERT< /code> 语句,其列名称如下所示。 *一条命令指定多个表:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

或:

pg_dump -U john -a --column-inserts --table=person --table=animal apple > backup.sql

或:

pg_dump -U john -a --column-inserts --table person --table animal apple > backup.sql

最后比如只导出apple<的特定表personanimal的数据/code> 用户(角色)john 的数据库到 backup.sql,仅使用 INSERT 语句,其列名称如下所示:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

然后,您可以使用 backup.sql 导入到用户(角色)johnorange 数据库.org/docs/current/app-psql.html" rel="nofollow noreferrer">psql 必须用于导入非存档文件,如下所示。 *您必须创建 orange 数据库和架构,否则会出现错误并且 我的回答解释了如何创建数据库和我的回答解释了如何导入架构和数据:

psql -U john -f backup.sql orange

并且,例如,您仅导出和存档(-Fc)的数据用户(角色)johnapple数据库的特定表personanimalbackup.sql 仅包含 INSERT 语句,其列名称如下所示。 *我的回答解释了如何导出和存档数据库:

pg_dump -U john -Fc -a --column-inserts -t person -t animal apple > backup.sql

然后,您可以使用 pg_restore 必须用于导入存档文件,如下所示。 *您必须创建 orange 数据库和架构,否则会出现错误并且 我的回答解释了如何创建数据库和我的回答解释了如何导入存档文件:

pg_restore -U john -d orange < backup.sql

For example, you can export only the data of apple database of the user(role) john to backup.sql as shown below. *My answer explains how to export both schema and data:

pg_dump -U john -a apple > backup.sql

Or:

pg_dump -U john --data-only apple > backup.sql

And, you can export only the data of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below:

pg_dump -U john -a --column-inserts apple > backup.sql

And, you can export only the data of apple database of the user(role) john to backup.sql with only INSERT statement which doesn't have column names as shown below:

pg_dump -U john -a --inserts apple > backup.sql

And, you can export only the data of the specific tables person and animal of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below. *Specifying multiple tables are available in one command:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

Or:

pg_dump -U john -a --column-inserts --table=person --table=animal apple > backup.sql

Or:

pg_dump -U john -a --column-inserts --table person --table animal apple > backup.sql

Lastly for example, you export only the data of the specific tables person and animal of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below:

pg_dump -U john -a --column-inserts -t person -t animal apple > backup.sql

Then, you can import backup.sql to orange database of the user(role) john with psql which must be used to import non-archive files as shown below. *You have to create orange database and the schema otherwise there is error and my answer explains how to create a database and my answer explains how to import schema and data:

psql -U john -f backup.sql orange

And, for example, you export and archive(-Fc) only the data of the specific tables person and animal of apple database of the user(role) john to backup.sql with only INSERT statement which has column names as shown below. *My answer explains how to export and archive a database:

pg_dump -U john -Fc -a --column-inserts -t person -t animal apple > backup.sql

Then, you can import archive backup.sql to orange database of the user(role) john with pg_restore which must be used to import archive files as shown below. *You have to create orange database and the schema otherwise there is error and my answer explains how to create a database and my answer explains how to import archive files:

pg_restore -U john -d orange < backup.sql
咆哮 2024-09-08 15:17:57

要在没有 root 用户的情况下将数据集作为插入命令获取 - 使用不同的用户

pg_dump -U -a --column-inserts --data-only > backup.sql

数据将被备份到backup.sql文件中

To get the data set as insert commands without root user - using different user

pg_dump -U -a --column-inserts --data-only > backup.sql

data will be backed up into a backup.sql file

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