In PostgreSQL, you can use the \connect meta-command of the client tool psql:
\connect DBNAME
or in short:
\c DBNAME
Note that it is not possible to switch the current database via an SQL command, like you asked.
MySQL's USE DatabaseName is named somewhat confusingly, because what MySQL calls a "database" is more similar to what other systems (like Oracle or PostgreSQL) call a "schema" (and PostgreSQL also lets you access multiple schemas from a single SQL session). To quote the official documentation:
Answer from Will Hartung on Stack OverflowUsers coming from an Oracle Database background may find that the MySQL meaning of a database is closer to what Oracle Database calls a schema.
In PostgreSQL, you can use the \connect meta-command of the client tool psql:
\connect DBNAME
or in short:
\c DBNAME
Note that it is not possible to switch the current database via an SQL command, like you asked.
MySQL's USE DatabaseName is named somewhat confusingly, because what MySQL calls a "database" is more similar to what other systems (like Oracle or PostgreSQL) call a "schema" (and PostgreSQL also lets you access multiple schemas from a single SQL session). To quote the official documentation:
Users coming from an Oracle Database background may find that the MySQL meaning of a database is closer to what Oracle Database calls a schema.
You can connect to a database with \c <database> or \connect <database>.
PSQL - change default database from postgres to another database
plpgsql - PostgreSQL: How to switch database inside cursor script - Database Administrators Stack Exchange
[PostgreSQL] Programmatically switch database but not \c
postgresql - How to switch databases in postgres? - Stack Overflow
I know that it is possible since I changed the default database in the past but I forgot how I did. There is a file where one can change that, I think.
When you open the psql shell you have to type your own server, database, port etc or accept the default values by entering. And there is a way how to change these default values.
Can anybody help me please? :)
Thank you!
const client = new Client({database: 'postgres'});
await client.connect();
await client.query('CREATE DATABASE tester;');
await client.query("CREATE ROLE test_role WITH PASSWORD 'password' IN ROLE other_role LOGIN;");
await client.query('CREATE SCHEMA schemes AUTHORIZATION test_role;');
I'm trying to programmatically spin up a new testing database. Im working towards building experience with docker, and incorporating migrations, and as the code is currently written, I start up a client. create a db, user, and schema, then insert a bunch of tables. My issue is that I login to "postgres" DB, and the schema is created in "postgres" but I want it created in the new DB "tester". Besides logging out and back in, is there a way to programmatically switch databases, or create the schema in a database that user isn't currently logged into?
This is javascript, node, porsager/pg
Technically PostgreSQL can't switch databases. You must disconnect and reconnect to the new DB.
The psql command-line client will do this for you with the \connect command, shortcut \c. But these are not commands processed by the PostgreSQL server, they're client commands. Different clients won't understand or support them.
At a guess you're using PgAdmin-III, in which case use the pulldown menu in the query tool to switch databases.
Some day I'd like to extract psql's backslash-commands code into a library that things like PgAdmin could link to and use too.
Just I am adding the screenshot below, if we want to see the Postgres databases connection in SQL developer this would be work, I know not relavent to the Question, but somehow it gives the clarity while connecting the SQL developer.

