Postgres Creating a schema in a specific database

I have one suggestion like stackoverflow.com for creating a schema in a specific database as follows:

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

But I get the error as follows:

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

*** Error ***

ERROR: syntax error at or near "\"

Situation:

Post-election login
create user u1
create db1 database with owner u1
\ connect u1
create s1 circuit

Please help me in creating a schema in a specific db.

+6
source share
1 answer

This works if - and only if - you are 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 the psql command. This is not regular SQL, so you cannot use it in a .sql file. As far as I know, there is no way to do this from the sql file. The rationale for this is that you should reconnect to another database as soon as you want to use this other database. This is for dividing the database. The reason MySQL implemented the use $database syntax is due to the lack of a schema, but PostgreSQL does not have such a command, since the database must be shared in the schema, not in the databases.

+9
source

Source: https://habr.com/ru/post/922814/


All Articles