I have a shell script which extracts data from the website. I know, I can store this data into text or csv file and then import it into database using PostgreSQL "copy" command.
My interest is not to use this text or csv file in the process. But, directly copy the data into database.
Is there anyway to do this? Any suggestion are appreciated. Thank you
1 Answer 1
You can open a pipe and write your data to the pipe directly. Related answer with demo code:
Or you can use COPY
with a program or standard input directly:
COPY table_name [ ( column_name [, ...] ) ] FROM { 'filename' | PROGRAM 'command' | STDIN } [ [ WITH ] ( option [, ...] ) ]
...
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. WhenSTDIN
orSTDOUT
is specified, data is transmitted via the connection between the client and the server.
-
COPY profile FROM PROGRAM 'sh echo.sh' with DELIMITER ',' ;
Here is what i tried. But i am getting an errorprogram sh echo.sh failed
. Any suggestion on this. Here, echo.sh is just a script which displays firstname, lastname, age, SportsPreference.Swatesh Pakhare– Swatesh Pakhare2016年02月25日 22:03:12 +00:00Commented Feb 25, 2016 at 22:03
INSERT
statements, at a minimum. It would be helpful to know what programming language you're using as well.