33

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?

3 Answers 3

53

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

psql \
  -h $hostname -d $dbname -U $username \
  -c "\copy mytable (column1, column2)  from '/path/to/local/file.csv' with delimiter as ','"

https://www.postgresql.org/docs/current/sql-copy.html

Sign up to request clarification or add additional context in comments.

5 Comments

I tried above yours and Schwern's. it is working fine (from console). but, while I trying this from Java code, getting "\" syntax error.
Note that \copy is a psql command. It will not work in other contexts.
Just in case it helps another person. If you get "missing data for column... it's because you forgot to specify the delimiter.
Great Work...:-)
if you have comma in data (e.g., "123 main st, new york, usa") with delimiter as ',' does not work.. replace it with CSV instead
24

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.

Comments

-1

For local database, you will simply use:

COPY TABLENAME FROM 'D:\xyz.csv' DELIMITER ',' CSV HEADER

For Server (remote database), you have to add \ :

\COPY TABLENAME FROM 'D:\xyz.csv' DELIMITER ',' CSV HEADER

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.