postgres:创建表时不存在schema

时间:2017-05-15 12:20:16

标签: sql postgresql schema

你能不能在尝试创建这样的表时告诉你原因:

CREATE TABLE IF NOT EXISTS  doc.bgp_communities (

    i             SERIAL PRIMARY KEY,
    comm_name     TEXT NOT NULL,
    comm_value    TEXT NOT NULL,
    label         TEXT NOT NULL,
    used_as       TEXT NOT NULL,
        modified_on   TIMESTAMPZ DEFAULT NOW(),

    UNIQUE        ( comm_value ),
    CHECK         ( (comm_value SIMILAR TO '%:%') OR (comm_value SIMILAR TO '%:%:%') ),
    CHECK         ( used_as IN ('bgp-customer','destination','dtv-peer','free-peer','generic','infrastructure','origin','private-peer','public-peer','traffic-eng','voip-peer') )

) ;

在此之前,我已经按如下方式创建了模式:

ip_spotlight-# CREATE SCHEMA doc
ip_spotlight-# CREATE SCHEMA net
ip_spotlight-# ALTER DATABASE ip_spotlight SET search_path TO doc,net

我收到以下错误消息:

ERROR:  schema "doc" does not exist

你能告诉我我做错了吗?

PS:我的用户“app”配置如下:

app          | Superuser, No inheritance, Create role, Create DB, Replication | {}

修改

app@[local]:5432 ip_spotlight# CREATE SCHEMA doc ;
CREATE SCHEMA
Time: 1.492 ms
app@[local]:5432* ip_spotlight# CREATE SCHEMA net ;
CREATE SCHEMA
Time: 0.641 ms
app@[local]:5432* ip_spotlight# ALTER DATABASE ip_spotlight SET search_path TO doc,net ;
ALTER DATABASE
Time: 1.036 ms

# CREATE TABLE IF NOT EXISTS  doc.bgp_communities (
ip_spotlight(# i             SERIAL PRIMARY KEY,
ip_spotlight(# comm_name     TEXT NOT NULL,
ip_spotlight(# comm_value    TEXT NOT NULL,
ip_spotlight(# label         TEXT NOT NULL,
ip_spotlight(# used_as       TEXT NOT NULL,
ip_spotlight(# modified_on   TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
ip_spotlight(# UNIQUE        ( comm_value ),
ip_spotlight(# CHECK         ( (comm_value SIMILAR TO '%:%') OR (comm_value SIMILAR TO '%:%:%') ),
ip_spotlight(# CHECK         ( used_as IN ('bgp-customer','destination','dtv-peer','free-peer','generic','infrastructure','origin','private-peer','public-peer','traffic-eng','voip-peer') )
ip_spotlight(# ) ;
ERROR:  schema "doc" does not exist
Time: 0.686 ms
app@[local]:5432! ip_spotlight# \dn
ERROR:  current transaction is aborted, commands ignored until end of transaction block
app@[local]:5432! ip_spotlight# 

1 个答案:

答案 0 :(得分:1)

你在命令末尾错过了半冒号,所以你可能没有创建任何架构:

ip_spotlight-# CREATE SCHEMA doc
ip_spotlight-# CREATE SCHEMA net
ip_spotlight-# ALTER DATABASE ip_spotlight SET search_path TO doc,net

应该看:

t=# CREATE SCHEMA doc;
CREATE SCHEMA
t=# CREATE SCHEMA net;
CREATE SCHEMA
t=# ALTER DATABASE ip_spotlight SET search_path TO doc,net;
ALTER DATABASE