postgres:从命令行在数据库中创建表 [英] postgres: create table in database from the command line

查看:549
本文介绍了postgres:从命令行在数据库中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在postgres中创建一个表,但最终在错误的数据库中。

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

这就是我的工作:最初在sql脚本中创建用户和数据库,然后是表。代码将进一步说明:

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

并且我已经在系统上创建了postgres用户。

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

问题就来了:该表最终出现在postgres数据库公共模式中,而不是在sinfonifry数据库公共模式中。

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?

谢谢,欢呼吧。

推荐答案

在创建数据库命令之后发出连接:

After the create database command issue a connect:

create database sinfonifry owner sinfonifry;
\connect sinfonifry

这篇关于postgres:从命令行在数据库中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆