将PostgreSQL表中的特定行导出为INSERT SQL脚本 [英] Export specific rows from a PostgreSQL table as INSERT SQL script

查看:1265
本文介绍了将PostgreSQL表中的特定行导出为INSERT SQL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库模式,名为: nyummy 和一个名为 cimory 的表:

 创建表nyummy.cimory(
id numeric(10,0)not null,
名称字符变化(60)
city character vary(50)not null,
CONSTRAINT cimory_pkey PRIMARY KEY(id)
);

我想导出 cimory 表的数据作为插入SQL脚本文件。但是,我只想出口城市等于东京的记录/数据(假设城市数据全部为小写)。



如何做?



无论解决方案是在免费的GUI工具还是命令行都无关紧要(尽管GUI工具解决方案更好)。我已经尝试过pgAdmin III,但是我找不到这样做的选项。

解决方案

创建一个带有你的表要导出,然后使用命令行实用程序pg_dump导出到一个文件:

 创建表export_table为
选择id,name,city
from nyummy.cimory
where city ='tokio'



< pre class =lang-sh prettyprint-override> $ pg_dump --table = export_table --data-only --column-inserts my_database> data.sql

- 列插入将转储为具有列名称的插入命令。



- 仅数据不转储模式。 >

如下所述,创建一个视图而不是一个表将消除表创建,每当需要新的导出。


I have a database schema named: nyummy and a table named cimory:

create table nyummy.cimory (
  id numeric(10,0) not null,
  name character varying(60) not null,
  city character varying(50) not null,
  CONSTRAINT cimory_pkey PRIMARY KEY (id)
);

I want to export the cimory table's data as insert SQL script file. However, I only want to export records/data where the city is equal to 'tokyo' (assume city data are all lowercase).

How to do it?

It doesn't matter whether the solution is in freeware GUI tools or command line (although GUI tools solution is better). I had tried pgAdmin III, but I can't find an option to do this.

解决方案

Create a table with the set you want to export and then use the command line utility pg_dump to export to a file:

create table export_table as 
select id, name, city
from nyummy.cimory
where city = 'tokio'

$ pg_dump --table=export_table --data-only --column-inserts my_database > data.sql

--column-inserts will dump as insert commands with column names.

--data-only do not dump schema.

As commented below, creating a view in instead of a table will obviate the table creation whenever a new export is necessary.

这篇关于将PostgreSQL表中的特定行导出为INSERT SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆