Check if database exists in PostgreSQL using shell

前端 未结 14 1529
南笙
南笙 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:16

    postgres@desktop:~$ psql -l | grep  | wc -l
    

    This will return 1 if the database specified exists or 0 otherwise.

    Also, if you try to create a database that already exists, postgresql will return an error message like this:

    postgres@desktop:~$ createdb template1
    createdb: database creation failed: ERROR:  database "template1" already exists
    

提交回复
热议问题