【问题标题】:Simple Replication on postgresqlpostgresql 上的简单复制
【发布时间】:2014-09-07 20:43:15
【问题描述】:

我在一个数据库上创建了两个表,我需要将它们复制到另一个数据库但在同一台机器上。我该怎么做?

观察:

CREATE TABLE cities1 ( city varchar(80) primary key, location point );

CREATE TABLE weather10 ( city varchar(80) references cities1(city), temp_lo int, temp_hi int, prcp real, date date )

INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');//insere uma linha na tabela com os dados

INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');

【问题讨论】:

标签: postgresql replication database-replication


【解决方案1】:

如果database1和database2中已经存在表:

在数据库 1 上:

psql database1
copy cities1 to '/tmp/cities1.copy';
copy weather10 to '/tmp/weather10.copy';

在数据库 2 上:

psql database2
truncate cities1, weather10;
copy cities from '/tmp/cities1.copy';
copy weather10 from '/tmp/weather10.copy';

或者,从命令行,假设database2存在,但database2中不存在表:

pg_dump database1 -tcities1 -tweather10 | psql database2

如果表已存在于 database2 中,但未填充:

pg_dump -a database1 -tcities1 -tweather10 | psql database2

如果表已经存在并且database2中有数据:

echo 'truncate cities, weather10;' > /tmp/tfile
pg_dump -a database1 -tcities1 -tweather10 >> /tmp/tfile
psql database2 -f /tmp/tfile
rm /tmp/tfile

您甚至可以使用外部包装器或 dblink 完全在 database1 中进行复制操作。

想到的另一件事,您可以创建两个数据库并将一个复制到另一个,这将完成整个数据库,而不仅仅是两个表。

如果您只需要第二个数据库中的相同数据,您可以在第二个数据库中创建一个外部包装器,它只是引用第一个数据库中的数据(不复制它)。

可能还有其他方法:-)

祝你好运, -g

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2012-11-23
    • 2014-07-06
    相关资源
    最近更新 更多