How to check if a postgres user exists?

前端 未结 4 1732
一生所求
一生所求 2021-01-30 02:04

createuser allows creation of a user (ROLE) in PostgreSQL. Is there a simple way to check if that user(name) exists already? Otherwise createuser returns with an er

4条回答
  •  灰色年华
    2021-01-30 02:50

    Following the same idea than to check if a db exists

    psql -t -c '\du' | cut -d \| -f 1 | grep -qw 
    

    and you can use it in a script like this:

    if psql -t -c '\du' | cut -d \| -f 1 | grep -qw ; then
        # user exists
        # $? is 0
    else
        # ruh-roh
        # $? is 1
    fi
    

提交回复
热议问题