postgresql 将sql文件数据加载到postgres中的单个表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21307786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:16:52  来源:igfitidea点击:

Load sql file data in to single table in postgres

postgresqlrestoresql-insert

提问by Shiva Krishna Bavandla

I have sql file(single_table_data.sql) that contains data of only one table(I have taken dump of only one table from server)

我有一个 sql 文件(single_table_data.sql),它只包含一张表的数据(我只从服务器转储了一张表)

Now i have to insert this sql file in to only single table in my database,

现在我必须将这个 sql 文件插入到我的数据库中的单个表中,

so how to import sql file in to single table in postgres ?

那么如何将 sql 文件导入到 postgres 中的单个表中?

Edit

编辑

For example i had database name SpeedDataand table name CurrentTable, so now i want to insert entire sql file data in to this table CurrentTable

例如我有数据库名SpeedData和表名CurrentTable,所以现在我想将整个 sql 文件数据插入到这个表中CurrentTable

Note: The sql file contains only insert statements(not even create statements)

注意:sql文件只包含insert statements(甚至不包含create语句)

回答by hage

From the documentation:

文档

psql dbname < infile

This should create a new table with the name of the previously dumped one and insert all data into it. Replace dbnamewith the name of the new database and infilewith the name/path of the file containing the dump, in your case (single_table_data.sql)

这应该使用先前转储的名称创建一个新表,并将所有数据插入其中。替换dbname为新数据库infile的名称和包含转储的文件的名称/路径,在您的情况下 ( single_table_data.sql)

If the dump contains only the insert statements, create the table by hand and then execute psql -U your_username -d dbname -f single_table_data.sql

如果转储仅包含插入语句,则手动创建表然后执行 psql -U your_username -d dbname -f single_table_data.sql

回答by awaik

If I understood correctly you want to create table from file and fill in with data. Correct command is

如果我理解正确,您想从文件创建表并填写数据。正确的命令是

PGPASSWORD=<password> psql -f /home/.../filename.sql -h localhost -d database_name -U user_name