使用 PostgreSQL 在数据库之间传输数据

发布于 2024-09-05 19:30:18 字数 268 浏览 3 评论 0 原文

我需要从另一个数据库传输一些数据。旧数据库名为 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

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

I need to transfer some data from another database. The old database is called paw1.moviesDB and the new database is paw1. The schema of each table are the following.

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

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

How do I copy the data from old database to the new database?

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

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

发布评论

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

评论(11

还如梦归 2024-09-12 19:30:18

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

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

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

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

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

完成!

I just had to do this exact thing so I figured I'd post the recipe here. This assumes that both databases are on the same server.

First, copy the table from the old db to the new db. At the commandline:

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

Next, grant permissions of the copied table to the user of the new database. Log into psql:

psql -U postgres -d <new_database>

ALTER TABLE <old_table> OWNER TO <new_user>;

\q

At this point your copied table in your new database still has the name <old_table> from your old database. Assuming you want to move the data somewhere else, say to <new_table>, you can just use regular SQL queries:

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

Done!

木槿暧夏七纪年 2024-09-12 19:30:18

PostgreSQL 中的数据库是隔离的;当您连接到 PostgreSQL 服务器时,您仅连接到一个数据库,您无法使用 SQL 查询将数据从一个数据库复制到另一个数据库。

如果您来自 MySQL:MySQL(宽松地)所谓的“数据库”是“schemas" - 某种名称空间。 PostgreSQL 数据库可以有许多模式,每个模式都有自己的表和视图,您可以使用 schema.table 语法从一种模式复制到另一种模式。

如果您确实有两个不同的 PostgreSQL 数据库,则将数据从一个数据库传输到另一个数据库的常见方法是导出表(使用 pg_dump -t )到文件,并将它们导入到另一个数据库(使用 psql)。

如果您确实需要从不同的 PostgreSQL 数据库获取数据,Grant Johnson 的回答中提到的另一个选项是 dblink,这是一个附加模块(在 contrib/ 中)。

更新:

Postgres 在 9.1 中引入了“外部数据包装器”(在提出问题后发布)。外部数据包装器允许通过 外部表 "https://www.postgresql.org/docs/current/postgres-fdw.html" rel="noreferrer">Postgres FDW 可以访问远程表(在不同的服务器和数据库上)就好像它是本地表一样。

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.

像极了他 2024-09-12 19:30:18

这对我有用,可以将表从本地主机远程复制到 Heroku 的 postgresql:

pg_dump -C -t source_table -h localhost source_db | psql -h 目的地主机 -U 目的地用户 -p 目的地端口 目的地数据库

这会为您创建表。

另一个方向(从 Heroku 到本地)
pg_dump -C -t 源表 -h 源主机 -U 源用户 -p 源端口 源数据库 | psql -h 本地主机目的地数据库

This worked for me to copy a table remotely from my localhost to Heroku's postgresql:

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

This creates the table for you.

For the other direction (from Heroku to local)
pg_dump -C -t source_table -h source_host -U source_user -p source_port source_db | psql -h localhost destination_db

寄风 2024-09-12 19:30:18

来自: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;

From: hxxp://dbaspot.c om/postgresql/348627-pg_dump-t-give-where-condition.html (NOTE: the link is now broken)

# 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;

Another method useful in remotes

  # 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;
倾城°AllureLove 2024-09-12 19:30:18

如果这是一次性的,可以使用三个选项来复制它:

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

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

  1. 更改同一数据库中的模式
  2. db_link

There are three options for copying it if this is a one off:

  1. Use a db_link (I think it is still in contrib)
  2. Have the application do the work.
  3. Export/import

If this is an ongoing need, the answers are:

  1. Change to schemas in the same DB
  2. db_link
北城半夏 2024-09-12 19:30:18
  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 命令。)

这应该可以做到。

  1. If your source and target database resides in the same local machine, you can use:

Note:- Sourcedb already exists in your database.

CREATE DATABASE targetdb WITH TEMPLATE sourcedb;

This statement copies the sourcedb to the targetdb.

  1. If your source and target databases resides on different servers, you can use following steps:

Step 1:- Dump the source database to a file.

pg_dump -U postgres -O sourcedb sourcedb.sql

Note:- Here postgres is the username so change the name accordingly.

Step 2:- Copy the dump file to the remote server.

Step 3:- Create a new database in the remote server

CREATE DATABASE targetdb;

Step 4:- Restore the dump file on the remote server

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

(pg_dump is a standalone application (i.e., something you run in a shell/command-line) and not an Postgres/SQL command.)

This should do it.

淡淡的优雅 2024-09-12 19:30:18

不能像SQL Server那样进行跨数据库查询; PostgreSQL 不支持这一点。

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

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

You can not perform a cross-database query like SQL Server; PostgreSQL does not support this.

The DbLink extension of PostgreSQL is used to connect one database to another database. You have install and configure DbLink to execute a cross-database query.

I have already created a step-by-step script and example for executing cross database query in PostgreSQL. Please visit this
post: PostgreSQL [Video]: Cross Database Queries using the DbLink Extension

掀纱窥君容 2024-09-12 19:30:18

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

描述(全部在 Linux 服务器上完成):

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

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

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

    创建或替换函数 send_data( VARCHAR )
    返回字符随 AS 变化
    $身体$
    我的 $command = $_[0] || die '没有 SQL 命令!';
    我的 $connection_string =
    “dbi:Pg:dbname=your_dbase;主机=192.168.1.2;端口=5432;”;
    $dbh = DBI->connect($connection_string,'用户','通行证',
    {自动提交=>0,RaiseError=>1,PrintError=>1,pg_enable_utf8=>1,}
    );
    我的 $sql = $dbh->准备($命令);
    评估{ $sql->执行() };
    我的 $error = $dbh->状态;
    $sql->结束;
    if ( $error ) { $dbh->; rollback() } else { $dbh->;犯罪() }
    $dbh->断开();
    $身体$
    语言 plperlu VOLATILE;
    

然后您可以在数据库 A 中调用该函数:

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

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

Actually, there is some possibility to send a table data from one PostgreSQL database to another. I use the procedural language plperlu (unsafe Perl procedural language) for it.

Description (all was done on a Linux server):

  1. Create plperlu language in your database A

  2. Then PostgreSQL can join some Perl modules through series of the following commands at the end of postgresql.conf for the database A:

    plperl.on_init='use DBI;'
    plperl.on_init='use DBD::Pg;'
    
  3. You build a function in A like this:

    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;
    

And then you can call the function inside database A:

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

And the value "zzzzzz" will be added into table "jm" in database B.

小ぇ时光︴ 2024-09-12 19:30:18

使用 dbeaver,传输数据是小菜一碟。
执行步骤

  1. 选择您想要传输数据的所有表。
    免责声明:如果我们单击其中所有表格的表格文件夹
    r 目前它不起作用。

  2. 右键单击并选择导入。

  3. 选择
    “导入源”中的表:
    输入图片此处描述

  4. 在“选择输入对象”中
    从您想要获取对应数据的位置选择数据库源
    每个表名称。

  5. 单击“下一步”并继续。

我接受问候。

Using dbeaver, transferring data is a piece of cake.
Steps to do

  1. Select all the tables u want the data to be transferred.
    Disclaimer: if we click on table folder inside which all the tables
    r present it won't work.

  2. Right click and select import.

  3. Choose
    Table in "import source":
    enter image description here

  4. In "select input object"
    choose db source from where u want to get the data coorosponding to
    each table name.

  5. Click on next and proceed.

I accept for Greetings.

深居我梦 2024-09-12 19:30:18

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

因此,我使用 \copy 到元命令导出到 CSV 并导入到目标数据库。

I think that use of the pg_dump utility can be restricted by a PostgreSQL Server admin.

So I used \copy to meta commands to export to CSV and import into destination database.

遗忘曾经 2024-09-12 19:30:18

就像 leonbloy 所建议的那样,在数据库中使用两个模式是正确的方法。假设有一个架构(旧数据库)和一个目标架构(新数据库),您可以尝试这样的操作(您应该考虑列名称、类型等):

INSERT INTO target.Awards SELECT * FROM source.Nominations;

Just like leonbloy suggested, using two schemas in a database is the way to go. Suppose a source schema (old DB) and a target schema (new DB), you can try something like this (you should consider column names, types, etc.):

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