如何将csv数据插入到postgresql数据库(远程数据库) [英] How to insert csv data into postgresql db (remote db)
问题描述
我有一个csv文件。
I have a csv file.
如果文件和数据库可以在同一台服务器上使用以下命令,我可以插入数据。
I could insert the data if file and db is available in same server using below command.
psql -h localhost -d local_mydb -U myuser -c "copy mytable (column1, column2) from '/path/to/local/file.csv' with delimiter as ','"
但是文件在本地服务器中,但db在另一个服务器(远程)中可用
But file is in local server but, db is available in another server (remote)
如果我尝试对远程服务器使用,请使用以下命令
If i try to do for remote server, using below command
psql -h remotehost -d remote_mydb -U myuser -c "copy mytable (column1, column2) from '/path/to/local/file.csv' with delimiter as ','"
取得权限被拒绝例外。
我该如何做?
推荐答案
\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屋!