Postgres: insert new row in table if the table does not have records where column_name = 'abc' occurs N times?
You can do something with triggers
Or you do somewhat complexer INSERT
Which allows only 5 John in your table


CREATE TABLE &quot;user&quot; (&quot;first_name&quot; varchar(16),&quot;last_name&quot; varchar(50) )


✓




insert into &quot;user&quot; 
select 'john', 'doe' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5




1 rows affected




insert into &quot;user&quot; 
select 'john', 'doe2' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5




1 rows affected




insert into &quot;user&quot; 
select 'john', 'doe3' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5


1 rows affected




insert into &quot;user&quot; 
select 'john', 'doe4' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5


1 rows affected




insert into &quot;user&quot; 
select 'john', 'doe5' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5


1 rows affected




insert into &quot;user&quot; 
select 'john', 'doe6' 
where (select count(*) from &quot;user&quot; where &quot;first_name&quot; = 'john') &lt; 5


✓




SELECT * FROM &quot;user&quot;


first_name | last_name
:--------- | :--------
john       | doe      
john       | doe2     
john       | doe3     
john       | doe4     
john       | doe5     


db&lt;&gt;fiddle here