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

我需要编写一个 Sql 脚本,在我刚刚创建的数据库中创建一个 新的数据库 < em > AND 一个新的模式

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

我正在使用 PostgreSQL 9.0

107783 次浏览

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 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=> CREATE SCHEMA s1;

To list the schema :

SELECT * from information_schema.schemata;

Create database using --CREATE DATABASE test;

Enter to the test database using --psql -d test;

Create your schema in test database using --create schema if not exists test_schema;