如何将表添加到 postgreSQL 中的特定模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41737720/
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 02:30:03  来源:igfitidea点击:

How do I add tables to a specific schema in postgreSQL?

sqlpostgresqlschema

提问by Jane Doe

I have this:

我有这个:

DROP SCHEMA Lab1 CASCADE;
CREATE SCHEMA Lab1;
CREATE TABLE Lab1.PERSONS(
SSN   INT primary key,
Name CHARACTER (30),
HouseId  INT,
ApartmentNumber  INT ,
Salary   DECIMAL (5, 2)  
);

But this doesn't work, it creates the schema fine but then creates the table in a weird place where it can't be viewed (with \d+for example), in fact, the only way I know it exists is if you try to drop the "lab1" schema it will throw an error saying that schema is being used by lab1.

但这不起作用,它可以很好地创建模式,然后在无法查看的奇怪位置(\d+例如)创建表,事实上,我知道它存在的唯一方法是如果您尝试删除“lab1”模式它会抛出一个错误,说明lab1正在使用模式。

I tried setting the default path to the lab1 schema: ALTER ROLE -myusername- SET SEARCH_PATH to Lab1?

我尝试设置 lab1 架构的默认路径: ALTER ROLE -myusername- SET SEARCH_PATH to Lab1?

But that didn't work either, \d+still has "persons" in public.

但这也没有用\d+,公共场合仍然有“人”。

So, how do I get this table into the schema I want? Thanks.

那么,如何将这个表放入我想要的模式中呢?谢谢。

回答by Vao Tsun

tried runnig:

尝试运行:

DROP SCHEMA Lab1 CASCADE;
CREATE SCHEMA Lab1;
CREATE TABLE Lab1.PERSONS(
SSN   INT primary key,
Name CHARACTER (30),
HouseId  INT,
ApartmentNumber  INT ,
Salary   DECIMAL (5, 2)  
);
t=# \dt+ lab1.persons
 lab1   | persons | table | postgres | 0 bytes |

As you can see the table is created in lab1 schema. If you want it in Lab1schema, you should modify your statemet:

如您所见,该表是在 lab1 模式中创建的。如果你想在Lab1模式中使用它,你应该修改你的 statemet:

t=#     DROP SCHEMA "Lab1" CASCADE;
ERROR:  schema "Lab1" does not exist
t=#     CREATE SCHEMA "Lab1";
CREATE SCHEMA
t=#     CREATE TABLE "Lab1".PERSONS(
t(#     SSN   INT primary key,
t(#     Name CHARACTER (30),
t(#     HouseId  INT,
t(#     ApartmentNumber  INT ,
t(#     Salary   DECIMAL (5, 2)
t(#     );
CREATE TABLE
t=#     \dt+ "Lab1".persons
 Lab1   | persons | table | postgres | 0 bytes |

回答by Praveen Kumar C

Choose the database and right click then choose the option "Query tool" and in which you want to create a table in particular a particular schema

选择数据库并右键单击然后选择选项“查询工具”,并在其中创建一个表,特别是特定模式

Choose the database and right click then choose the option "Query tool" and in which you want to create a table in particular a particular schema

选择数据库并右键单击然后选择选项“查询工具”,并在其中创建一个表,特别是特定模式