Check if database exists in PostgreSQL using shell

前端 未结 14 1515
南笙
南笙 2020-12-04 06:13

I was wondering if anyone would be able to tell me about whether it is possible to use shell to check if a PostgreSQL database exists?

I am making a shell script and

14条回答
  •  Happy的楠姐
    2020-12-04 06:53

    The following shell code seems to work for me:

    if [ "$( psql -tAc "SELECT 1 FROM pg_database WHERE datname='DB_NAME'" )" = '1' ]
    then
        echo "Database already exists"
    else
        echo "Database does not exist"
    fi
    

提交回复
热议问题