SQL PostgreSQL:在特定数据库中创建模式

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

PostgreSQL: Create schema in specific database

sqldatabaseschemapostgresql

提问by Axel Fontaine

I need to write an sql scriptthat creates both a new database ANDa new schemain the database I just created.

我需要编写一个sql 脚本,在我刚刚创建的数据库中创建一个新数据库一个新模式

How can I do it? Can I somehow change the current database to the new one? Or can I somehow specify the database for CREATE SCHEMA?

我该怎么做?我可以以某种方式将当前数据库更改为新数据库吗?或者我可以以某种方式为 CREATE SCHEMA 指定数据库吗?

I'm using PostgreSQL 9.0

我正在使用 PostgreSQL 9.0

回答by Berry Langerak

You can connect to the database, and execute the "CREATE SCHEMA" statement. That should result in a new schema in that database. It's not as tough as you think ;) When you want to do this from a .SQL file instead, you can use the \connect command as such:

您可以连接到数据库,并执行“CREATE SCHEMA”语句。这应该会在该数据库中产生一个新模式。这并不像你想象的那么难;) 当你想从一个 .SQL 文件中做到这一点时,你可以使用 \connect 命令:

 CREATE DATABASE foo;
 \connect foo;
 CREATE SCHEMA yourschema;

回答by Kiruthika kanagarajan

Login to New-Database with new user:

使用新用户登录新数据库:

postgres=> \connect newdb user1
...
You are now connected to database "newdb" as user "user1".
newdb=> 

To create schema with new user "user1" in newdb:

在 newdb 中使用新用户“user1”创建模式:

newdb=> CREATE SCHEMA s1;

To list the schema :

列出架构:

SELECT * from information_schema.schemata;