Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert CSV data into PostgreSQL database (remote database )

Tags:

csv

postgresql

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?

like image 990
Spike Avatar asked Oct 26 '15 19:10

Spike


People also ask

How do I import a CSV file into PostgreSQL database using PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.


2 Answers

\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 ','"

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

like image 194
Neil McGuigan Avatar answered Oct 08 '22 07:10

Neil McGuigan


You can feed the file via STDIN. From the PostgreSQL COPY documentation...

When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.

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

I was incorrect about using FROM PROGRAM. It has the same caveats as FROM 'filename'. The server executes the program, not the client.

When PROGRAM is specified, the server executes the given command and reads from the standard output of the program, or writes to the standard input of the program. The command must be specified from the viewpoint of the server, and be executable by the PostgreSQL user.

like image 20
Schwern Avatar answered Oct 08 '22 09:10

Schwern