Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

Adding and populating column in Postgres with data from existing column

$
0
0

I have a TEXT column in one of my existing database tables that holds both domain names and IP addresses. I’d like to add a new column to the existing table that’s of type INET and populate it with the IP address from rows who’s TEXT column is an IP so I can take advantage of the IP Address and IP Network features in Postgres.

Adding the column is easy enough, but when I go to run UPDATE foo SET ip_addr = data::inet I (expectedly) get an ERROR: invalid input syntax for type inet for the first row that’s a domain name an the update stops.

Is there any way to skip the rows that aren’t IP addresses, or am I going to have to write a function for this? I’m needing to do it as part of a migration using a Golang migration tool (github.com/mattes/migrate) so I was hoping the UPDATE command would be sufficient.


Viewing all articles
Browse latest Browse all 1138

Trending Articles