2

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.

a_horse_with_no_name
  • 440,273
  • 77
  • 685
  • 758
Fi3n1k
  • 693
  • 3
  • 10
  • 20

1 Answers1

3

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';
Vao Tsun
  • 37,644
  • 8
  • 70
  • 98
  • hmm I don't think the company wants to add a table for this purpose. Thanks anyway for the suggestion – Fi3n1k Jan 31 '18 at 14:38
  • 1
    @Fi3n1k: well, queries can only work with tables. There is no way you can use those CSV values unless you create some kind of table. You could put it into a temporary table if you want – a_horse_with_no_name Jan 31 '18 at 14:41