postgresql Postgres 数据库如果不存在则创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15815372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:51:50  来源:igfitidea点击:

Postgres database create if not exists

sqldatabasepostgresqlddl

提问by Konstantin Tarashchanskiy

Is there an analog to CREATE TABLE IF NOT EXISTSfor creating databases?

是否有CREATE TABLE IF NOT EXISTS用于创建数据库的类比?

Background: I am writing a script to automatically set up the schema in PostgreSQL on an unknown system. I am not sure if the database (or even part of the schema) was already deployed, so I want to structure my code to not fail (or ideally even show errors) if some of the structure already exists. I want to differentiate the errors that prevent me from creating a database (so abort future schema changes since they will not work) from this error.

背景:我正在编写一个脚本,以在未知系统上的 PostgreSQL 中自动设置模式。我不确定数据库(甚至是架构的一部分)是否已经部署,所以如果某些结构已经存在,我想构建我的代码不会失败(或者理想情况下甚至显示错误)。我想从这个错误中区分阻止我创建数据库的错误(因此中止未来的架构更改,因为它们将不起作用)。

采纳答案by Eelke

No but you could query the pg_catalog.pg_database table to see if it exists.

不,但您可以查询 pg_catalog.pg_database 表以查看它是否存在。