Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres: create table in database from the command line

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.

like image 587
Ferenc Deak Avatar asked Mar 06 '13 15:03

Ferenc Deak


People also ask

How do you create a table in a database in pgAdmin?

Login to phpPgAdmin and reach "Public" database. Now click on "Create table" in the right hand pane of the phpPgAdmin window. In the next window, supply name and number of columns you want to create and click "Next". In the next window, supply name of the column, data type and click "Create".

How do you create a table in Greenplum?

To be able to create a table, you must have USAGE privilege on all column types or the type in the OF clause, respectively. If you specify a schema name, Greenplum creates the table in the specified schema. Otherwise Greenplum creates the table in the current schema.

Which command creates a database in PostgreSQL?

createdb creates a new PostgreSQL database. Normally, the database user who executes this command becomes the owner of the new database. However, a different owner can be specified via the -O option, if the executing user has appropriate privileges. createdb is a wrapper around the SQL command CREATE DATABASE .


1 Answers

After the create database command issue a connect:

create database sinfonifry owner sinfonifry;
\connect sinfonifry
like image 172
Clodoaldo Neto Avatar answered Oct 15 '22 06:10

Clodoaldo Neto