问题描述
我收到了一个来自stackoverflow.com的建议表,用于在特定数据库中创建架构,如下所示:
I got one suggestion form stackoverflow.com to create schema in a specific database as follows:
CREATE DATABASE foo;
\connect foo;
CREATE SCHEMA yourschema;
但是我收到如下错误:
错误: \处或附近的语法错误LINE 2:\connect foo;
^
*** 错误 ** *
*** Error ***
错误: \处或附近的语法错误
ERROR: syntax error at or near "\"
情况:
以postgress登录
创建用户u1
创建数据库具有所有者u1的db1
u连接u1
创建模式s1
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.
推荐答案
如果-且仅当-您使用的是psql,则此方法有效:
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
是psql命令。这不是普通的SQL,因此您不能在.sql文件中使用它。据我所知,没有办法从sql文件中执行此操作。其基本原理是,一旦要使用其他数据库,就应重新连接到该数据库。这是用于数据库分离。 MySQL之所以实现 use $ database
语法,是因为它缺少模式,但是PostgreSQL没有这样的命令,因为数据库应该以模式的方式分开,而不是数据库。
\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.
这篇关于Postgres在特定数据库中创建架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!