0

Please is there a manner to create user if not exit.

CREATE USER sup_ WITH PASSWORD 'postgres';

When i executed two times got error saying that sup_ exist. How should i do please to correct my query ?

Thank you?

hamam
  • 47
  • 7
  • 1
    Does this answer your question? [Create PostgreSQL ROLE (user) if it doesn't exist](https://stackoverflow.com/questions/8092086/create-postgresql-role-user-if-it-doesnt-exist) – Oz Heymann Jan 31 '21 at 20:19

2 Answers2

0

There's no IF NOT EXISTS clause for CREATE ROLE. You'll probably want to use PL/pgSQL to solve your problem. Have a look at this question, it's basically the same one and the answers are really thorough.

Oz Heymann
  • 534
  • 5
  • 13
0

First check whether the user exists or not to avoid error. User below query:

Do
$do$
BEGIN
  IF NOT EXISTS (SELECT * FROM pg_user WHERE usename = 'sup_')then 
      CREATE USER sup_ WITH PASSWORD 'postgres';
   END IF;
end
$do$