Check if database exists in PostgreSQL using shell

前端 未结 14 1536
南笙
南笙 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条回答
  •  心在旅途
    2020-12-04 07:03

    I'm still pretty inexperienced with shell programming, so if this is really wrong for some reason, vote me down, but don't be too alarmed.

    Building from kibibu's answer:

    # If resulting string is not zero-length (not empty) then...
    if [[ ! -z `psql -lqt | cut -d \| -f 1 | grep -w $DB_NAME` ]]; then
      echo "Database $DB_NAME exists."
    else
      echo "No existing databases are named $DB_NAME."
    fi
    

提交回复
热议问题