4

I'm trying to execute following Postgres 9.6 commands over JDBC connection

CREATE USER my_db WITH SUPERUSER PASSWORD 'my_db';
CREATE DATABASE my_db;
GRANT ALL PRIVILEGES ON DATABASE my_db TO my_db;

\connect my_db; -- THIS ONE FAILS
SET ROLE my_db;

CREATE SCHEMA my_db AUTHORIZATION my_db;

"\connect" command fails as not recognized. Is there way to connect other database staying within the same JDBC connection?

UPD: "CONNECT TO ..." and "EXEC SQL CONNECT TO ..." also fail.

8
  • The documentation should help you: Connecting to the DB over JDBC Commented Oct 18, 2017 at 18:21
  • Did you notice that I already have JDBC connection? Commented Oct 18, 2017 at 18:31
  • How could I notice that when you haven't shared any JDBC / Java code? It is also unclear to me what you're attempting to do here. Commented Oct 18, 2017 at 18:33
  • Having fresh server and 'postgres' superuser, I want to create another database and schema within this new database staying within the same JDBC connection. I know it is possible to do it using two subsequent JDBC connections. However, it is not the option for me Commented Oct 18, 2017 at 18:38
  • 1
    Opening a new connection is the only way you can do this. Commented Oct 18, 2017 at 18:49

1 Answer 1

4

Backslash commands are not PostgreSQL SQL commands, they're commands in the psql command-line utility. Behind the scenes, \connect just closes the connection and opens a new one.

PostgreSQL its self does not have any way to switch databases on a connection.

Disconnect and reconnect to the other DB.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.