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.
- More details in the always useful manual here.
- Here is a related case on pgsql-general.