0

I have two tables in PostgreSQL:

Table indexces_grid:

+---------------+------------+
| Column Name   | Type       |
+---------------+------------+
| indexid       | bigint     |
| iprange       | int8range  |
+---------------+------------+

Table citydetails:

+---------------+-----------+
| Column Name   | Type      |
+---------------+-----------+
| ipfrom        | bigint    |
| ipto          | bigint    |
| cityname      | varchar   |
+---------------+-----------+

I want to insert the ipfrom and ipto into the single column iprange of the indexces_grid table.

1 Answer 1

2

It's so simple:

INSERT INTO indexces_grid ("Row2")
SELECT int8range(ipfrom, ipto, '[]')
  FROM citydetails;

The documentation linked above tells you all.

Note that one has to use the three-parameter form of the range constructor function (here int8range()) to include the upper bound, too. Using the two-parameter form, the upper bound would be non-inclusive.

I've created an SQLFiddle to show this at work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.