如果不存在,Postgres数据库创建 [英] Postgres database create if not exists
问题描述
是否有用于创建数据库的 CREATE TABLE IF NOT EXISTS
的模拟?
Is there an analog to CREATE TABLE IF NOT EXISTS
for creating databases?
背景:我在写一个脚本来在未知系统上自动设置PostgreSQL中的模式。我不知道数据库(甚至部分模式)是否已经部署,所以我想结构我的代码,如果一些结构已经存在,不会失败(或理想情况下甚至显示错误)。我想区分错误,阻止我创建一个数据库(所以中止未来的模式更改,因为他们不会工作)从这个错误。
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.
推荐答案
否,但您可以查询pg_catalog.pg_database表以查看其是否存在。
No but you could query the pg_catalog.pg_database table to see if it exists.
这篇关于如果不存在,Postgres数据库创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!