title: PostgreSQL create a schema under a different database using a script
body: <p>So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema. </p>
  
  <p>The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.</p>
  
  <pre><code>CREATE ROLE "new_role" ... ;
  
  CREATE TABLESPACE "new_space"
    OWNER "new_role"
    LOCATION '/home/...';
  
  
  CREATE DATABASE "new_db"
      WITH OWNER = "new_role"
              TABLESPACE = "new_space";
  
  CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
  
  CREATE TABLE IF NOT EXISTS "schema"."new_table"( 
  
  ...  
  ) TABLESPACE "new_space";...
  
  ...
  </code></pre>
  
  <p>I already saw a solution with a <code>\connect foo;</code> but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running <code>\connect foo</code> in the terminal.</p>
  
  <p>Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?</p>