postgresql postgres:从命令行在数据库中创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15251054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
postgres: create table in database from the command line
提问by Ferenc Deak
I am trying to create a table in postgres, but it ends up in the wrong database.
我试图在 postgres 中创建一个表,但它最终出现在错误的数据库中。
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:
这是我所做的:在我的 sql 脚本中,最初我创建了一个用户和一个数据库,然后是一个表。代码会解释更多:
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.
我已经在系统上创建了 postgres 用户。
And here comes the problem: the table ends up in the postgres database, public schema, not in the sinfonifry database, public schema.
问题来了:表最终出现在 postgres 数据库的公共架构中,而不是 sinfonifry 数据库的公共架构中。
How can I create the table in the database I want to?
如何在我想要的数据库中创建表?
Thanks, and cheers, f.
谢谢,干杯,f。
回答by Clodoaldo Neto
After the create database command issue a connect:
在 create database 命令发出连接后:
create database sinfonifry owner sinfonifry;
\connect sinfonifry