[英]postgresql Create Table based on columns of remote data
I want to copy a table from a remote database to my local Postgres
database. 我想将表从远程数据库复制到本地Postgres
数据库。 I face these constraints: 我面临以下限制:
view
. 该表是view
的结果。 pg_dump
is out of the option, I believe. 因此,我相信pg_dump
是pg_dump
。 I find out that I can use COPY
with STDIN
and STDOUT
in Postgres
. 我发现我可以在Postgres
中将COPY
与STDIN
和STDOUT
一起使用。 However, COPY
works only if the table on the local database is already created (if I understand correctly). 但是, COPY
仅在本地数据库上的表已经创建的情况下才起作用(如果我理解正确的话)。
There are about a thousand columns in the table I want to copy. 我要复制的表中大约有一千列。 Obviously, I don't want to type in all the columns and data types in CREATE TABLE
. 显然,我不想在CREATE TABLE
键入所有列和数据类型。 What's a good solution for this? 有什么好的解决方案?
The COPY
commands (if table is created) I use are as follows (Python Interface): 我使用的COPY
命令(如果已创建表)如下(Python接口):
copyremote = "COPY (SELECT * FROM {}.{} {}) TO stdout".format(dbname, tbname, condi)
copylocal = "COPY {} FROM stdin".format(tbname)
command = ['psql', '-h', remotepg, '-U', username, '-p', port, '-d', database, '-c', copyremote]
p1 = subprocess.Popen(command, stdout=subprocess.PIPE)
p2 = subprocess.Popen(['psql', dbname, '-c', copylocal], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
return p2.communicate()[0]
One way would be to export the table to a csv, rsync the csv to your local machine and import the csv back to your postgres by so, 一种方法是将表格导出到csv,将csv同步到本地计算机,然后将csv导入回postgres,这样,
Import and Export with a header to preserve the datatypes of the table as so. 带有标题的导入和导出可照此保留表的数据类型。
Import CSV to table with Header for columns present in first row of csv file. 将CSV格式的CSV文件导入到带有标题的表中。
COPY <TargetTableName> FROM '/path/to/csv/SourceCSVFile.csv' DELIMITERS ',' CSV HEADER
Export a table to CSV with Headers present in the first row. 将表格导出到CSV,并将第一行中包含标题。
COPY <SourceTableName> TO '/path/to/csv/TargetCSVFile' DELIMITERS ',' CSV HEADER;
If you have access to the table alone on the remote postges DB: you could use the -t option to pg_dump only the particular table 如果您可以单独访问远程postges DB上的表:可以使用-t选项仅将特定表pg_dump
pg_dump -U your_rolename -d <database_name> -t <table_name> > file.sql
After some research, here is the solution I'm using: 经过研究,这是我正在使用的解决方案:
Hopefully this can be useful for someone in similar situations. 希望这对处于类似情况的人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.