The following query inserts the IP address if there is none and returns the ID.
WITH variables AS (
SELECT '1.1.1.2'::inet AS ip
)
INSERT INTO globals.ips (ip)
SELECT v.ip
FROM variables AS v
WHERE NOT EXISTS (
SELECT i.global_ip_id
FROM globals.ips AS i
WHERE i.ip = v.ip
)
RETURNING global_ip_id
However, when the value is already present in the database it does not return the ID.
How can I get the ID when the value is already present?
I'm running PostgreSQL 12.