简体   繁体   English

Postgresql:检查架构是否存在?

[英]Postgresql: Check if Schema Exists?

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.如果我 go 创建一个已经存在的模式,我想(有条件地,通过外部方式)删除并按照指定重新创建它。 How can I check for the existence of said schema on my Postgres 9 server?如何检查我的 Postgres 9 服务器上是否存在所述模式?

Currently, I'm doing this:目前,我正在这样做:

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

but I feel like there's probably another way... is this the "proper" way to query Postgres for the existence of a particular schema?但我觉得可能还有另一种方式......这是查询 Postgres 是否存在特定模式的“正确”方式吗?

The following query will tell you whether a schema exists.以下查询将告诉您架构是否存在。

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

If you are a total purist or you want to gain some milisecs.如果你是一个纯粹的纯粹主义者,或者你想获得一些毫秒。 I recommend you to make use of postgres native system catalog.我建议您使用 postgres 本机系统目录。 One can avoid then nested loop which is caused by calling pg_catalog anyway...可以避免由于调用 pg_catalog 引起的嵌套循环......

SELECT EXISTS(SELECT 1 FROM information_schema.schemata 
              WHERE schema_name = 'name');

查询信息_schema

If you querying pg_namespace directly:如果您直接查询 pg_namespace:

SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'name');

Planer's work is much simpler: Planer 的工作要简单得多:

在此处输入图像描述

So your own solution was the best .所以你自己的解决方案是最好的

Somewhat related and perhaps of interest to others looking for conditional schema creation.与寻找条件模式创建的其他人有些相关并且可能感兴趣。 I found myself using code like this in some of my creation scripts:我发现自己在一些创建脚本中使用了这样的代码:

DO $$
BEGIN

    IF NOT EXISTS(
        SELECT schema_name
          FROM information_schema.schemata
          WHERE schema_name = 'pgcrypto'
      )
    THEN
      EXECUTE 'CREATE SCHEMA pgcrypto';
    END IF;

END
$$;

This can be one of the approaches.这可以是其中一种方法。 Drop the schema first and then create it.先删除模式,然后再创建它。

IF EXISTS:
Do not throw an error if the schema does not exist. A notice is issued in this case.

So,所以,

DROP SCHEMA IF EXISTS schema_Name
Create SCHEMA schema_Name

If you want to create a schema if it doesn't exist you can just execute:如果你想创建一个不存在的模式,你可以执行:

CREATE SCHEMA IF NOT EXISTS foo

Source: https://www.postgresql.org/docs/current/sql-createschema.html资料来源: https://www.postgresql.org/docs/current/sql-createschema.html

From http://www.postgresql.org/docs/9.1/static/infoschema-schemata.html (emphasis my own):来自http://www.postgresql.org/docs/9.1/static/infoschema-schemata.html (强调我自己的):

The view schemata contains all schemas in the current database that are owned by a currently enabled role .视图模式包含当前数据库中由当前启用的角色拥有的所有模式。

So your original solution/query is more reliable than Peter's, albeit non-standard.因此,您的原始解决方案/查询比彼得的更可靠,尽管是非标准的。

Use利用

SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspowner <> 1 AND nspname = 'schemaname');

If you check https://www.postgresql.org/docs/current/static/infoschema-schemata.html , you see如果您检查https://www.postgresql.org/docs/current/static/infoschema-schemata.html ,您会看到

The view schemata contains all schemas in the current database that the current user has access to (by way of being the owner or having some privilege).视图模式包含当前用户有权访问的当前数据库中的所有模式(通过成为所有者或具有某些特权)。

This means the query in accepted answer using information_schema.schemata doesn't show schemas that the current user isn't the owner of or doesn't have the USAGE privilege on.这意味着使用information_schema.schemata在接受的答案中的查询不显示当前用户不是所有者或没有USAGE权限的模式。

SELECT 1
FROM pg_catalog.pg_namespace
WHERE nspowner <> 1 -- ignore tables made by postgres itself
AND nspname = 'schemaname';

is more complete and will show all existing schemas that postgres didn't make itself regardless of whether or not you have access to the schema.更完整,并且将显示 postgres 自己没有创建的所有现有模式,无论您是否有权访问该模式。

This one worked for me (Postgres 9.3):这个对我有用(Postgres 9.3):

Select exists (SELECT 1 FROM information_schema.schemata where catalog_name = 'My_BD_with_UpperCase_characters_in_its_Name')

This is valid for the PostgreSQL that will check if the schema if exist and if not then will create it:这对 PostgreSQL 有效,它将检查模式是否存在,如果不存在,则创建它:

CREATE SCHEMA IF NOT EXISTS tenant;

NONE of those will work if you have objects (tables,sprocs,views) within a particular schema - IT WILL FAIL during DROP...如果您在特定模式中有对象(表、存储过程、视图),那么这些都不会起作用 - 在 DROP 期间它会失败......

CREATE & MANAGE is the easy part.. It's the drop that will get you.. Anyways, I couldn't find a suitable answer, so I posted here for others.. CREATE & MANAGE 是最简单的部分.. 它会让你失望.. 无论如何,我找不到合适的答案,所以我在这里发布给其他人..

SEE LINK HERE: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/4753d1b8-f547-44c6-b205-aa2dc22606ba/#6eb8238a-305e-40d5-858e-0fbd70454810此处查看链接: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/4753d1b8-f547-44c6-b205-aa2dc22606ba/#6eb8238a-305e-40d5-8458e-0fbd704

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM