postgres 命令行建数据库表,postgres:从命令行在数据库中创建表

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.

解决方案

After the create database command issue a connect:

create database sinfonifry owner sinfonifry;

\connect sinfonifry

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值