Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a csv file from a url to Postgresql

Is there any way to use copy command for batch data import and read data from a url. For example, copy command has a syntax like :

COPY sample_table
FROM 'C:\tmp\sample_data.csv' DELIMITER ',' CSV HEADER;

What I want is not to give a local path but a url. Is there any way?

like image 240
Stathis Andronikos Avatar asked Jan 17 '17 12:01

Stathis Andronikos


People also ask

How do I import a CSV file into PostgreSQL using Python?

First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2. connect() method. before importing a CSV file we need to create a table. In the example below, we created a table by executing the “create table” SQL command using the cursor.


1 Answers

It's pretty straightforward, provided you have an appropriate command-line tool available:

COPY sample_table FROM PROGRAM 'curl "http://www.example.com/file.csv"'

Since you appear to be on Windows, I think you'll need to install curl or wget yourself. There is an example using wget on Windows here which may be useful.

like image 133
Nick Barnes Avatar answered Oct 01 '22 06:10

Nick Barnes