PostgreSQLdata validation

Validate IPv4 Address in PostgreSQL Using Regex

Validate IPv4 addresses in PostgreSQL using the regex ~ operator. SQL examples for IP address format validation in Postgres.

The Regex Pattern

Regex pattern

^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

Use this pattern with PostgreSQL's ~ operator.

PostgreSQL SQL Example

-- Use the ~ regex match operator in PostgreSQL to validate IPv4 address format in an ip_address column
SELECT *
FROM your_table
WHERE your_column ~ '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$';

How This Works

Use the ~ regex match operator in PostgreSQL to validate IPv4 address format in an ip_address column. The regex pattern is used with PostgreSQL's ~ operator to filter rows at the database level.

Using regex directly in SQL is efficient for use cases like data validation โ€” it avoids loading data into application memory just to filter it with a regex.

Need a custom SQL + Regex query?

Describe your specific use case in plain English. RegSQL generates the exact PostgreSQL query with the right regex pattern for your schema.

Related SQL Patterns