Postgres Creating Schema in a specific database

♀尐吖头ヾ 提交于 2019-12-23 08:00:09

问题


I got one suggestion form stackoverflow.com to create schema in a specific database as follows:

CREATE DATABASE foo; 
 \connect foo; 
CREATE SCHEMA yourschema; 

But I am getting error as follows:

ERROR: syntax error at or near "\" LINE 2: \connect foo; ^

*** Error ***

ERROR: syntax error at or near "\"

Situation :

Login as postgress
create user u1
create database db1 with owner u1
\connect u1
create schema s1

Please help me in creating schema in a specific db.


回答1:


This works, if - and only if - you're using psql:

postgres@berry-pc:~$ psql template1
psql (8.4.10)
Type "help" for help.

template1=# CREATE DATABASE foo;
CREATE DATABASE
template1=# \connect foo;
psql (8.4.10)
You are now connected to database "foo".
foo=# \connect template1
psql (8.4.10)
You are now connected to database "template1".
template1=# DROP DATABASE foo;
DROP DATABASE
template1=# \q

\connect is a psql command. It's not normal SQL, so you can't use that in a .sql file. There is no way to do it from within a sql file, as far as I know. The rationale behind this, is that you should reconnect to a different database once you want to use that different database. This is for database separation. The reason MySQL has implemented the use $database syntax is because of it's lack of schema's, but PostgreSQL doesn't have a command like that, as a database should be separated in schema's, rather than databases.



来源:https://stackoverflow.com/questions/11949401/postgres-creating-schema-in-a-specific-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!