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.

Answer from Will Hartung on Stack Overflow
🌐
Liquid Web
liquidweb.com › home › listing and switching databases in postgresql
List of Databases in PostgreSQL: How to Switch and Navigate | Liquid Web
January 15, 2026 - Step 4: Switching Between Databases in PostgreSQL Switching between databases is another way of saying you are closing one connection and opening another.
🌐
SQLines
sqlines.com › postgresql › pgadmin › change_database
PostgreSQL pgAdmin - How to Change Database in Query Tool - SQLines Tools
In PostgreSQL you can work with a single database only, and there is no a USE dbname statement to change the database, you have to close the current connection and then connect to another database.
🌐
CommandPrompt Inc.
commandprompt.com › education › postgresql-listing-and-switching-databases-in-psql
PostgreSQL: Listing and Switching Databases in psql — CommandPrompt Inc.
February 1, 2023 - Alternatively, you can use the “\connect” command followed by the database name to switch the databases in psql:
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
PostgreSQL
postgresql.org › message-id › v02140b77b1efa8a7d563@[207.152.64.133]
PostgreSQL: Switch database inside psql?
August 6, 1998 - I get an error message when I try createdb from the command line, but once I've done psql -u, I can do it, so I guess if I had to do this from some sort of Perl/sed/awk/??? script (none of which I could write anyway), I'd need to know how to convince createdb that I *am* a valid database-creator, by golly.
🌐
Reddit
reddit.com › r/sql › [postgresql] programmatically switch database but not \c
r/SQL on Reddit: [PostgreSQL] Programmatically switch database but not \c
February 16, 2025 -
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

Find elsewhere
🌐
EDUCBA
educba.com › home › data science › data science tutorials › postgresql tutorial › postgres switch database
Postgres Switch Database | Example of Postgres Switch Database
May 25, 2023 - ... Saying that database doesn’t exist. ... We can switch between the databases in PostgreSQL using \c or \connect metacommand, which creates a new connection and closes the current one.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Reddit
reddit.com › r/postgresql › psql - change default database from postgres to another database
r/PostgreSQL on Reddit: PSQL - change default database from postgres to another database
January 8, 2022 -

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!

🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › postgresql-alter-database
PostgreSQL ALTER DATABASE - GeeksforGeeks
July 22, 2024 - The features of a database, once created can be changed using the ALTER DATABASE statement.
🌐
freeCodeCamp
freecodecamp.org › news › manage-postgresql-with-psql
How to Manage PostgreSQL Databases from the Command Line with psql
October 3, 2025 - You’re currently still connected to the default postgres database. To connect to a database or to switch between databases, use the \c command.
🌐
w3resource
w3resource.com › PostgreSQL › snippets › use-database-command-postgresql.php
How to Use the equivalent of "USE database_name" in PostgreSQL
December 23, 2024 - To "switch" databases in PostgreSQL, you must either: Exit the current session and connect to a new database, or · Use \c database_name within the psql command-line interface.
🌐
ComputingForGeeks
computingforgeeks.com › home › using ai coding agents for devops: terraform, ansible, and kubernetes with opencode
Using AI Coding Agents for DevOps: Terraform, Ansible, and Kubernetes with OpenCode | ComputingForGeeks
April 9, 2026 - #!/usr/bin/env bash set -euo pipefail # Configuration S3_BUCKET="s3://company-backups/postgresql" BACKUP_DIR="/var/backups/postgresql" RETENTION_DAYS=7 DATE_PREFIX=$(date +%Y-%m-%d) LOG_TAG="pg-backup" PG_USER="postgres" log() { logger -t "$LOG_TAG" "$1" echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" } cleanup() { local exit_code=$? if [[ $exit_code -ne 0 ]]; then log "ERROR: Backup failed with exit code $exit_code" fi exit $exit_code } trap cleanup EXIT mkdir -p "$BACKUP_DIR" log "Starting PostgreSQL backup" # Get list of databases, excluding templates DATABASES=$(sudo -u "$PG_USER" psql -At -c \ "
🌐
Moodle
download.moodle.org › releases › latest
Latest release | Moodle downloads
May 13, 2026 - Install Moodle on your own server (requires a web server with PHP and a database) by downloading one of the following packages or obtaining Moodle via Git.
🌐
Atlassian
atlassian.com › data › sql › how to create a copy of a database in postgresql
Create a copy of a Database in PostgreSQL | Atlassian
This will open a new window with a connection: Now that a connection has been established, we can begin writing queries. You can switch to other databases by typing “\c [Database Name]”. To look at all the databases, the \list or \l meta-command can be used:
🌐
XDA Developers
xda-developers.com › home › ai tools › i connected claude code to my home server through mcp, and now i manage my entire lab by talking to it
I connected Claude Code to my home server through MCP, and now I manage my entire lab by talking to it
March 30, 2026 - It removed the friction of knowing where to look for information. Whether the process was slow or fast, didn't matter. I didn’t need to find the container that held Immich’s database, or what the SQL structure and the credentials to Postgres were.
🌐
Microsoft
microsoft.com › en-us › sql-server
Microsoft SQL Server
Drive transformation with Azure Accelerate for Databases.
🌐
Hostman
hostman.com › tutorials › how to migrate a postgresql database to another server
How to Migrate a PostgreSQL Database to Another Server | Hostman
January 22, 2026 - Import (Target): psql -U username -h localhost -d new_db < backup.sql · Big Bang (Offline): The entire system is taken offline, data is exported, moved, and imported. It is simple but requires significant downtime. Trickle (Replication/Zero-Downtime): The new database is set up as a replica of the old one. Once they are synchronized, you switch the application to the new DB.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Prisma
prisma.io
Prisma ORM
Start with Prisma ORM for type-safe database access, add Prisma Postgres for managed PostgreSQL, and deploy TypeScript apps for APIs and AI agents on Prisma Compute.