Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL copy command using STDIN

Tags:

sql

postgresql

I need to load the data from the CSV file to table in PostgreSQL. and I'm not a superuser to use the copy command. when i read few topics from the postgreSQL site I came to know abut the \copy command using STDIN and STDOUT.

I have tried with the same but getting errors. what actually I was trying is I have CSV file located in 'D:/test/test.csv' trying to load in tablename:test by using the below copy command

command: \copy test from stdin.

  1. what is exactly STDIN and where I have to assign the file path

  2. And one more doubt do I need to run this command only in psql or i can run this in SQL workbench.

like image 817
Rajesh Avatar asked Dec 11 '22 08:12

Rajesh


1 Answers

1) stdin is standard input - means you have to paste (or type) the data

2) yes \copy is psql meta-command, not SQL, thus can be executed in psql only...

Performs a frontend (client) copy. This is an operation that runs an SQL COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server, and no SQL superuser privileges are required.

also - you don't have to run from stdin, below should work as well:

\copy test from 'D:/test/test.csv'
like image 56
Vao Tsun Avatar answered Dec 27 '22 03:12

Vao Tsun