PostgreSQL:检查模式是否存在? [英] Postgresql: Check if Schema Exists?

查看:311
本文介绍了PostgreSQL:检查模式是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要即时创建,管理和删除模式。如果我要创建一个已经存在的模式,那么我想(有条件地通过外部方式)删除并按照指定的方式重新创建它。如何在Postgres 9服务器上检查是否存在上述模式?

I need to create, manage and drop schemas on the fly. If I go to create a schema that already exists, I want to (conditionally, via external means) drop and recreate it as specified. How can I check for the existence of said schema on my Postgres 9 server?

当前,我正在这样做:

select exists (select * from pg_catalog.pg_namespace where nspname = 'schemaname');

但是我觉得可能还有另一种方式……这是查询Postgres的正确方式

but I feel like there's probably another way... is this the "proper" way to query Postgres for the existence of a particular schema?

推荐答案

以下查询将告诉您某个模式是否存在。

The following query will tell you whether a schema exists.

SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'name';

这篇关于PostgreSQL:检查模式是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆