Home > Software engineering >  What is the ideal datatype to be used to store IP address in rails postgresql app?
What is the ideal datatype to be used to store IP address in rails postgresql app?

Time:09-23

I have a requirement where I need to store an IP address in the table on a rails app. The format could be of IPv4 or IPv6. Currently I thought of storing them as 'strings'. But is this a good approach. Could there be a better alternative here that could accomodate both?

CodePudding user response:

The appropriate PostgreSQL data type is clearly inet.

While inet can also represent a subnet, it is also the appropriate type for a single IP address:

The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If the /y portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for IPv6, so the value represents just a single host.

CodePudding user response:

string works as well.

# Migration
t.string   :current_sign_in_ip

# Controller
user.current_sign_in_ip = request.remote_ip
  • Related