6

Trying to use 'copy' command of PostgreSQL in windows cli command:

COPY myTable FROM value.txt (DELIMITER('|'));

I can't find 'copy' executable file in bin directory.

Can you let me know how can I run 'copy' command in cli?

Added: My windows application is going to use the 'Copy' feature. Need to run it directly from Application.

Thanks in advance.

I could manage to see my required result with following approach.

psql.exe -f copy.sql -p 5433 -U user -s postgres

copy.sql
\copy TARGET_TABLE FROM source.txt (DELIMITER('#')); 
7
  • Might this SO article be of use to you? Commented Dec 18, 2015 at 4:08
  • It seems that it is for backing up all database? I just need to upload the text file into a table frequently. Thanks. Commented Dec 18, 2015 at 4:13
  • Are you looking to copy an entire table, or to INSERT into a table from a source text file? Commented Dec 18, 2015 at 4:14
  • There are several lines text in the source text file, and I want to upload those ones into the table. Looks like INSERT. Thanks. Commented Dec 18, 2015 at 4:18
  • Are you using any GUI like PgAdmin ? Commented Dec 18, 2015 at 6:29

2 Answers 2

5

You have the right command (COPY), but you need to begin an interactive session with Postgres if you want use the COPY command from the Windows command line.

psql -U username yourdb

This should leave you at a prompt looking like the following:

yourdb=#

Now you can use the COPY command and it should work:

COPY myTable FROM value.txt (DELIMITER('|'))

The problem you were having is that COPY is not a Windows executable program, it is a Postgres command which is only understood by Postgres.

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

3 Comments

I see. My windows application should use 'copy' feature directly, so I wonder if I can upload text file content without put 'COPY' command in the prompt.
You might be able to write a batch script which will begin the Postgres session behind the scenes. The input could be the COPY query you want to run.
I just used a "-f" option to access copy.sql.
0

For loading Json use the below command- \COPY myTable (data) FROM '~specifypath/data.json';

data is the column where you want to push the json

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.