64

我需要从另一个数据库传输一些数据。旧数据库称为 paw1.movi​​esDB,新数据库称为 paw1。每个表的架构如下。

Awards (name of the table)(new DB)
Id [PK] Serial           Award

Nominations (name of the table) (old DB)
Id [PK] Serial           nominations

如何将旧数据库中的数据复制到新数据库中?

4

10 回答 10

96

我只需要做这件事,所以我想我会把食谱贴在这里。这假设两个数据库都在同一台服务器上。

首先,将旧数据库中的表复制到新数据库。在命令行:

pg_dump -U postgres -t <old_table> <old_database> | psql -U postgres -d <new_database>

接下来,将复制的表的权限授予新数据库的用户。登录到 psql:

psql -U postgres -d <new_database>

ALTER TABLE <old_table> OWNER TO <new_user>;

\q

此时,您在新数据库中复制的表仍然具有<old_table>旧数据库中的名称。假设您想将数据移动到其他地方,例如<new_table>,您可以使用常规 SQL 查询:

INSERT INTO <new_table> (field1, field2, field3) 
SELECT field1, field2, field3 from <old_table>;

完毕!

于 2012-03-29T16:35:38.993 回答
42

Databases are isolated in PostgreSQL; when you connect to a PostgreSQL server you connect to just one database, you can't copy data from one database to another using a SQL query.

If you come from MySQL: what MySQL calls (loosely) "databases" are "schemas" in PostgreSQL - sort of namespaces. A PostgreSQL database can have many schemas, each one with its tables and views, and you can copy from one schema to another with the schema.table syntax.

If you really have two distinct PostgreSQL databases, the common way of transferring data from one to another would be to export your tables (with pg_dump -t ) to a file, and import them into the other database (with psql).

If you really need to get data from a distinct PostgreSQL database, another option - mentioned in Grant Johnson's answer - is dblink, which is an additional module (in contrib/).

Update:

Postgres introduced "foreign data wrapper" in 9.1 (which was released after the question was asked). Foreign data wrappers allow the creation of foreign tables through the Postgres FDW which makes it possible to access a remote table (on a different server and database) as if it was a local table.

于 2010-06-16T01:07:44.287 回答
25

这对我来说可以将一个表从我的本地主机远程复制到 Heroku 的 postgresql:

pg_dump -C -t source_table -h localhost source_db | psql -h destination_host -U destination_user -p destination_port destination_db

这将为您创建表格。

对于另一个方向(从 Heroku 到本地) pg_dump -C -t source_table -h source_host -U source_user -p source_port source_db | psql -h localhost destination_db

于 2013-11-20T18:05:32.597 回答
13

来自:hxxp://dbaspot.com/postgresql/348627-pg_dump-t-give-where-condition.html(注意:链接现在已断开

# create temp table with the data
psql mydb
CREATE TABLE temp1 (LIKE mytable);
INSERT INTO temp1 SELECT * FROM mytable WHERE myconditions;
\q

# export the data to a sql file
pg_dump --data-only --column-inserts -t temp1 mtdb > out.sql
psql mydb
DROP TABLE temp1;
\q

# import temp1 rows in another database
cat out.sql | psql -d [other_db]
psql other_db
INSERT INTO mytable (SELECT * FROM temp1);
DROP TABLE temp1;

另一种在遥控器中有用的方法

  # export a table csv and import in another database
  psql-remote> COPY elements TO '/tmp/elements.csv' DELIMITER ',' CSV HEADER;
  $ scp host.com:/tmp/elements.csv /tmp/elements.csv
  psql-local> COPY elements FROM '/tmp/elements.csv' DELIMITER ',' CSV;
于 2012-08-06T05:37:01.540 回答
9

如果这是一次性的,则可以选择三种复制方式:

  1. 使用 db_link(我认为它仍在 contrib 中)
  2. 让应用程序完成工作。
  3. 出口进口

如果这是一个持续的需求,答案是:

  1. 更改为同一数据库中的模式
  2. db_link
于 2010-06-17T18:18:32.680 回答
3
  1. 如果您的源数据库和目标数据库位于同一本地计算机上,您可以使用:

注意:- Sourcedb 已存在于您的数据库中。

CREATE DATABASE targetdb WITH TEMPLATE sourcedb;

此语句将 sourcedb 复制到 targetdb。

  1. 如果您的源数据库和目标数据库位于不同的服务器上,您可以使用以下步骤:

第 1 步:- 将源数据库转储到文件中。

pg_dump -U postgres -O sourcedb sourcedb.sql

注意:- 这里 postgres 是用户名,因此请相应地更改名称。

第 2 步:-将转储文件复制到远程服务器。

第 3 步:-在远程服务器中创建一个新数据库

CREATE DATABASE targetdb;

第 4 步:-在远程服务器上恢复转储文件

psql -U postgres -d targetdb -f sourcedb.sql

(pg_dump 是一个独立的应用程序(即,你在 shell/命令行中运行的东西)而不是 Postgres/SQL 命令。)

这应该这样做。

于 2020-07-15T07:11:43.367 回答
1

实际上,有可能将表数据从一个 PostgreSQL 数据库发送到另一个。我为此使用过程语言 plperlu(不安全的 Perl 过程语言)。

说明(全部在 Linux 服务器上完成):

  1. 在您的数据库中创建 plperlu 语言 A

  2. 然后PostgreSQL可以在数据库A的postgresql.conf末尾通过以下一系列命令加入一些Perl模块:

    plperl.on_init='use DBI;'
    plperl.on_init='use DBD::Pg;'
    
  3. 您在 A 中构建一个函数,如下所示:

    CREATE OR REPLACE FUNCTION send_data( VARCHAR )
    RETURNS character varying AS
    $BODY$
    my $command = $_[0] || die 'No SQL command!';
    my $connection_string =
    "dbi:Pg:dbname=your_dbase;host=192.168.1.2;port=5432;";
    $dbh = DBI->connect($connection_string,'user','pass',
    {AutoCommit=>0,RaiseError=>1,PrintError=>1,pg_enable_utf8=>1,}
    );
    my $sql = $dbh-> prepare( $command );
    eval { $sql-> execute() };
    my $error = $dbh-> state;
    $sql-> finish;
    if ( $error ) { $dbh-> rollback() } else {  $dbh-> commit() }
    $dbh-> disconnect();
    $BODY$
    LANGUAGE plperlu VOLATILE;
    

然后你可以调用数据库A里面的函数:

SELECT send_data( 'INSERT INTO jm (jm) VALUES (''zzzzzz'')' );

并且值“zzzzzz”将被添加到数据库B中的表“jm”中。

于 2015-12-18T10:14:08.440 回答
1

您不能像 SQL Server 那样执行跨数据库查询;PostgreSQL 不支持这个。

PostgreSQL 的 DbLink 扩展用于将一个数据库连接到另一个数据库。您已安装并配置 DbLink 以执行跨数据库查询。

我已经创建了一个分步脚本和示例,用于在 PostgreSQL 中执行跨数据库查询。请访问这篇文章:PostgreSQL [视频]:使用 DbLink 扩展的跨数据库查询

于 2015-08-22T11:40:22.017 回答
0

就像 leonbloy 建议的那样,在数据库中使用两个模式是可行的方法。假设一个模式(旧数据库)和一个目标模式(新数据库),你可以尝试这样的事情(你应该考虑列名、类型等):

INSERT INTO target.Awards SELECT * FROM source.Nominations;
于 2010-06-17T01:40:57.057 回答
0

我认为 pg_dump 实用程序的使用可能会受到 PostgreSQL 服务器管理员的限制。

所以我习惯使用\copy元命令导出到 CSV 并导入到目标数据库。

于 2021-11-09T11:43:06.737 回答