HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to insert an IP-address into an inet column in PostgreSQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlinetinsertaddresscolumnintohow

Problem

I would like to insert an IP-address into a column that has type inet. In what format can I insert the data? is it only binary or is there any way I can insert from text e.g. "192.168.1.082"? Are there any help-functions for this so I can test it from psql in the command prompt?

Solution

It seems pretty easy:

postgres=# create table inet_test (address inet);

CREATE TABLE

postgres=# insert into inet_test values ('192.168.2.1');

INSERT 0 1

postgres=# insert into inet_test values ('192.168.2.1/24');

INSERT 0 1

postgres=# select * from inet_test;

 address
----------------
 192.168.2.1
 192.168.2.1/24
(2 rows)

Code Snippets

postgres=# create table inet_test (address inet);

CREATE TABLE

postgres=# insert into inet_test values ('192.168.2.1');

INSERT 0 1

postgres=# insert into inet_test values ('192.168.2.1/24');

INSERT 0 1

postgres=# select * from inet_test;


 address
----------------
 192.168.2.1
 192.168.2.1/24
(2 rows)

Context

StackExchange Database Administrators Q#3086, answer score: 17

Revisions (0)

No revisions yet.