简体   繁体   English

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

[英]PostgreSQL: Create schema in specific database

I need to write an sql script that creates both a new database AND a new schema in 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

You can connect to the database, and execute the "CREATE SCHEMA" statement.您可以连接到数据库,并执行“CREATE SCHEMA”语句。 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:它没有你想象的那么难;)当你想从一个.SQL 文件中执行此操作时,你可以使用 \connect 命令:

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

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;

Create database using --CREATE DATABASE test;使用 --CREATE DATABASE 测试创建数据库;

Enter to the test database using --psql -d test;使用 --psql -d test 进入测试数据库;

Create your schema in test database using --create schema if not exists test_schema;使用 --create schema if not exists test_schema 在测试数据库中创建模式;

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

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