如何将CSV数据插入PostgreSQL数据库(远程数据库) [英] How to insert CSV data into PostgreSQL database (remote database )

查看:325
本文介绍了如何将CSV数据插入PostgreSQL数据库(远程数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件。如果文件与数据库位于同一服务器上,我可以使用以下命令将数据插入数据库:

I have a CSV file. I can, using the command below, insert the data into a database if file is on the same server as the database:

psql -h localhost -d local_mydb -U myuser -c "copy mytable (column1, column2)  from '/path/to/local/file.csv' with delimiter as ','"

但是文件位于本地服务器上,数据库位于另一台(远程)服务器上。

But the file is on a local server and the database is on another (remote) server.

如果我尝试对远程服务器执行此操作,请使用以下命令:

If I try to do this for a remote server, using the command below:

psql -h remotehost -d remote_mydb -U myuser -c "copy mytable (column1, column2)  from '/path/to/local/file.csv' with delimiter as ','"

我得到一个权限被拒绝的异常。

I get a permission-denied exception.

如何将本地文件中的数据插入远程服务器上的数据库中,没有超级用户权限?

How can I insert data from a local file into a database on a remote server, without superuser privileges?

推荐答案

\copy (请注意反斜杠) )可让您在远程数据库之间进行复制,而无需超级用户权限。

\copy (note the backslash) lets you copy to/from remote databases and does not require superuser privileges.

psql -h remotehost -d remote_mydb -U myuser -c "\copy mytable (column1, column2)  from '/path/to/local/file.csv' with delimiter as ','"

如果您使用的是Java,您可以在JDBC驱动程序中使用CopyManager: https:/ /jdbc.postgresql.org/documentation/publicapi/org/postgresql/copy/CopyManager.html

If you're using Java, you can use the CopyManager in the JDBC driver: https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/copy/CopyManager.html

这篇关于如何将CSV数据插入PostgreSQL数据库(远程数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆