PostgreSQL inet / cidr Subnet Calculator
Enter an inet or cidr value and see exactly what PostgreSQL stores and prints: host(), masklen(), network(), broadcast(), netmask(), hostmask() and abbrev(), the number of addresses and the usable host range. Add a second address to evaluate the <<, <<=, >>, >>= and && containment operators and inet_merge(). Parsing follows the rules of PostgreSQL's inet and cidr input functions, including classful defaults for cidr values written without a mask and the "Value has bits set to right of mask" error, so you can check a value before it reaches your database. Everything runs in your browser.
Do more than postgresql inet / cidr subnet calculator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Choose inet (an address with an optional netmask) or cidr (a network), then type a value such as 192.168.10.37/24 or 2001:db8::/48.
- Optionally enter a second address to test containment operators and inet_merge(), then click Calculate.
- Read the function results and copy the generated SQL, including the GiST index that speeds up <<= lookups.
Frequently asked questions
What is the difference between inet and cidr in PostgreSQL?
inet stores a host address together with an optional netmask, so 192.168.10.37/24 is a valid inet value. cidr stores a network: every bit to the right of the mask must be zero, so 192.168.10.37/24 is rejected and 192.168.10.0/24 is accepted. Use inet for device or client addresses and cidr for subnets and allow lists.
How do I fix 'invalid cidr value: Value has bits set to right of mask'?
The address has host bits set beyond the prefix length. Either use the network address (network('192.168.10.37/24'::inet) returns 192.168.10.0/24), shorten the mask so it matches the address, or store the column as inet if you need to keep the host part.
How can I query IP ranges in PostgreSQL efficiently?
Use the containment operators, for example WHERE ip <<= '10.20.0.0/16', and create a GiST index with the inet_ops operator class. Chat2DB can connect to your PostgreSQL database and write these queries with its AI assistant: download it from chat2db.ai/download or use the web version at app.chat2db.ai.
