Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL query from csv file [duplicate]

What is the easiest way to query from .csv file ?

Ex: I have a list of 1000 email addresses in emails.csv file.

I want to query all users with emails that are in emails.csv file ex:

SELECT * 
FROM users 
WHERE email IN (emails.csv)

Is there a way to do like this something or I need to create a script. If some script is needed, can you please provide some example.

like image 895
Fi3n1k Avatar asked Sep 10 '25 19:09

Fi3n1k


1 Answers

you need to create a table and copy it from csv, smth like:

t=# create table csv(i int,email text);
CREATE TABLE
t=# copy csv(email) from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> q
>> w
>> \.
COPY 2
t=# select * from csv;
 i | email
---+-------
   | q
   | w
(2 rows)

but In your case you copy from file, not STDIN, eg

copy csv(email) from '/path/to/file';
like image 196
Vao Tsun Avatar answered Sep 13 '25 08:09

Vao Tsun