简体   繁体   English

Postgres:pg_restore/pg_dump 一切,除了表的表 id 的表

[英]Postgres : pg_restore/pg_dump everything EXCEPT the table id's for a table

Currently I'm doing something like:目前我正在做类似的事情:

pg_dump -a -O -t my_table my_db > my_data_to_import.sql

What I really want is to be able to import/export just the data without causing conflicts with my autoid field or overwriting existing data.我真正想要的是能够只导入/导出数据而不会与我的 autoid 字段发生冲突或覆盖现有数据。

Maybe I'm thinking about the whole process wrong?也许我在想整个过程是错误的?

You can use COPY with column list to dump and restore just data from one table.您可以将COPY与列列表一起使用,以仅从一个表中转储和恢复数据。 For example:例如:

COPY my_table (column1, column2, ...) TO 'yourdumpfilepath';
COPY my_table (column1, column2, ...) FROM 'yourdumpfilepath';

OID is one of the system columns . OID 是系统列之一。 For example it is not included in SELECT * FROM my_table (you need to use SELECT oid,* FROM my_table ).例如它不包含在SELECT * FROM my_table中(您需要使用SELECT oid,* FROM my_table )。 OID is not the same as ordinary id column created along with other columns in CREATE TABLE . OID 与在CREATE TABLE中与其他列一起创建的普通 id 列不同。 Not every table has OID column.并非每个表都有 OID 列。 Check default_with_oids option.检查default_with_oids选项。 If it's set to off, then probalby you don't have OID column in your table, but even if so, then you can still create table with OID using WITH OIDS option.如果它设置为关闭,那么很可能您的表中没有 OID 列,但即使是这样,您仍然可以使用WITH OIDS选项创建具有 OID 的表。 It's recommended not to use OID as table's column (that's why default_with_oids is set to off prior to PostgreSQL 8.1).建议不要使用 OID 作为表的列(这就是为什么在 PostgreSQL 8.1 之前将 default_with_oids 设置为关闭的原因)。

pg_dump --insert -t TABLENAME DBNAME > fc.sql
       cat fc.sql | sed -e 's/VALUES [(][0-9][0-9],/VALUES (/g'|sed -e 's/[(]id,/(/g' > fce.sql
       psql -f fce.sql DBNAME

This dumps the table with columns into fc.sql then uses sed to remove the id, and the value associated with it这会将带有列的表转储到 fc.sql 然后使用 sed 删除 id 以及与之关联的值

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

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