3

I have a table with details of data transfers. One of the fields is an IP associated with the transfer. I need to develop a query which will get me a subset of the rows in the table matching one of 79 IP's. There are 608 distinct IP's in the table.

I have a file which has the required IP's separated by newlines. Is there a way to develop a query which reads this file to get the required rows instead of me manually entering each of the IP's separated by an OR?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
shaun
  • 560
  • 1
  • 11
  • 29

1 Answers1

10

If you have the text with IPs separated by newlines in the database or your client, this query would do the job:

Transform the list to an array, unnest it and join to the main table:

SELECT *
FROM  (SELECT unnest(string_to_array(your_list_of_ips, E'\n')) AS ip) sub
JOIN  data_transfers d USING (ip);

More about the used function in the manual here.

SQL COPY

To import from a file directly, you could use COPY. The data file has to be on the same machine as Postgres and you need to be a database superuser for this.

This time we already have a single IP per row:

CREATE TEMP TABLE tmp(ip text);

COPY tmp FROM '/path/to/file';

SELECT *
FROM  tmp
JOIN  data_transfers d USING (ip);

psql \copy

If your file is on a different machine or if you do not have superuser privileges, use the (mostly) equivalent \copy of psql instead. To do it from the bash (like requested in the comment):

psql dbname

dbname=# \set ips `cat ips.txt`

dbname=# SELECT *
dbname-# FROM  (SELECT unnest(string_to_array(:'ips', E'\n')) AS ip) sub
dbname-# JOIN  data_transfers d USING (ip);

\set is the psql meta-command to set a variable - to the contents of a file in this case.
ips.txt being your file with IPs.
:'ips' is the syntax for single-quoted SQL interpolation.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Thanks. The copy option does not work since it requires me to be superuser. Where would I run the first option? Because if I run it after connecting to the database using psql I get an error column "ip" specified in USING clause does not exist in right table. Is there a way to do this from the command line on the client using psql? – shaun Feb 14 '14 at 18:16
  • @shaun: I expanded on psql `\copy`. – Erwin Brandstetter Feb 14 '14 at 23:42