If the line does not exist, insert else do not insert in postgres

I need to check if a string exists or not. If it does not exist, it must be inserted.

This is in postgres , and I'm trying to insert a line through a shell script. When I run the script, it does not show an error, but does not insert into the table, even if the corresponding row is missing.

+7
source share
1 answer

I like the solution mentioned here

 INSERT INTO table (id, field, field2) SELECT 3, 'C', 'Z' WHERE NOT EXISTS (SELECT 1 FROM table WHERE id=3); 
+21
source

All Articles