Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to create a table in postgres, but it ends up in the wrong database.

Here is what I do: in my sql script initially I create a user and a database, and then a table. Code will explain more:

drop database if exists sinfonifry;
drop role if exists sinfonifry;

-- create the requested sinfonifry user

create user sinfonifry createdb createuser password 'some_password';

-- create a sinfonifry database
create database sinfonifry owner sinfonifry;

DROP TABLE if exists sinf01_host;

CREATE TABLE sinf01_host
(
  host_id bigserial NOT NULL, -- The primary key
  host_ip inet NOT NULL, -- The IP of the host
  host_name character varying(255) NOT NULL, -- The name of the host
  CONSTRAINT "PK_host" PRIMARY KEY (host_id )
)
WITH (
  OIDS=FALSE
);

ALTER TABLE sinf01_host OWNER TO sinfonifry;

now, this is part an automated script, and is executed from the command line, like:

sudo -u postgres psql < create_db.sql

and I already have the postgres user created on the system.

And here comes the problem: the table ends up in the postgres database, public schema, not in the sinfonifry database, public schema.

How can I create the table in the database I want to?

Thanks, and cheers, f.

share|improve this question
    
Connect to the freshly created database before creating any objects in it. – wildplasser Mar 6 '13 at 15:18
up vote 17 down vote accepted

After the create database command issue a connect:

create database sinfonifry owner sinfonifry;
\connect sinfonifry
share|improve this answer
    
Excellent, Works :) – fritzone Mar 7 '13 at 7:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.