Create PostgreSQL ROLE (user) if it doesn't exist

前端 未结 10 1771
旧时难觅i
旧时难觅i 2020-12-12 15:47

How do I write an SQL script to create a ROLE in PostgreSQL 9.1, but without raising an error if it already exists?

The current script simply has:

CR         


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 16:23

    You can do it in your batch file by parsing the output of:

    SELECT * FROM pg_user WHERE usename = 'my_user'
    

    and then running psql.exe once again if the role does not exist.

提交回复
热议问题