我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?
当前回答
您还可以使用pgAdmin II中的备份功能。只需遵循以下步骤:
In pgAdmin, right click the table you want to move, select "Backup" Pick the directory for the output file and set Format to "plain" Click the "Dump Options #1" tab, check "Only data" or "only Schema" (depending on what you are doing) Under the Queries section, click "Use Column Inserts" and "User Insert Commands". Click the "Backup" button. This outputs to a .backup file Open this new file using notepad. You will see the insert scripts needed for the table/data. Copy and paste these into the new database sql page in pgAdmin. Run as pgScript - Query->Execute as pgScript F6
工作良好,可以做多个表在一个时间。
其他回答
Pg_dump并不总是有效。
假设在两个dbs中有相同的表ddl 你可以从stdout和stdin中破解它,如下所示:
# grab the list of cols straight from bash
psql -d "$src_db" -t -c \
"SELECT column_name
FROM information_schema.columns
WHERE 1=1
AND table_name='"$table_to_copy"'"
# ^^^ filter autogenerated cols if needed
psql -d "$src_db" -c \
"copy ( SELECT col_1 , col2 FROM table_to_copy) TO STDOUT" |\
psql -d "$tgt_db" -c "\copy table_to_copy (col_1 , col2) FROM STDIN"
你可以这样做:
pg_dump -h <host ip address> -U <host db user name> -t <host table> > <host database> | psql -h localhost -d <local database> -U <local db user>
检查这个python脚本
python db_copy_table.py "host=192.168.1.1 port=5432 user=admin password=admin dbname=mydb" "host=localhost port=5432 user=admin password=admin dbname=mydb" alarmrules -w "WHERE id=19" -v
Source number of rows = 2
INSERT INTO alarmrules (id,login,notifybyemail,notifybysms) VALUES (19,'mister1',true,false);
INSERT INTO alarmrules (id,login,notifybyemail,notifybysms) VALUES (19,'mister2',true,false);
作为一种替代方法,您还可以使用外部数据包装器扩展将远程表公开为本地表。然后,您可以通过从远程数据库中的表进行选择,将数据插入到您的表中。唯一的缺点是速度不是很快。
如果你有两个远程服务器,那么你可以这样做:
pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase
它会将源数据库中提到的表复制到目标数据库中同名的表,如果您已经有了模式。
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录