PostgreSQL: Create schema in specific database

Axel Fontaine picture Axel Fontaine · Jun 28, 2011 · Viewed 71.2k times · Source

I need to write an sql script that creates both a new database AND a new schema in the database I just created.

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?

I'm using PostgreSQL 9.0

Answer

Berry Langerak picture Berry Langerak · Jun 28, 2011

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;