How to USE in PostgreSQL

Galaxy Glossary

How do I replicate MySQL’s USE command in PostgreSQL?

Changes your psql session to a different PostgreSQL database, similar to MySQL’s USE command.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why doesn’t USE work in PostgreSQL?

PostgreSQL treats databases as separate server-level objects; standard SQL has no USE command. Instead, psql offers the meta-command \c (or \connect) to switch databases within the same server connection.

How do I switch databases in psql?

Run \c target_db (or \c target_db username if you need a different role). The prompt changes to confirm the new database.

Can I switch databases inside a SQL script?

Add \c target_db on its own line. Because \c is a psql directive, the file must be executed with psql, not via a client library that only interprets SQL.

How do I reconnect with a URL?

If you are outside psql, reconnect using your driver’s connection string: postgresql://user:pass@host:5432/target_db. Switching requires a new connection.

What are common use cases?

1. Jumping between ecommerce_dev and ecommerce_prod during debugging.
2. Running maintenance tasks on a staging database without opening a new terminal.
3. Loading data into throw-away scratch databases for ad-hoc analysis.

Best practices for switching databases

• Use dedicated roles per environment (app_dev, app_prod) and pass the role to \c.
• Keep automated scripts environment-specific; avoid mid-script switches to reduce risk.
• In CI pipelines, open a fresh connection rather than \c to make logs clearer.

How do I verify the current database?

Run SELECT current_database(); or check the psql prompt, which shows dbname=>.

Why How to USE in PostgreSQL is important

How to USE in PostgreSQL Example Usage


-- Start in development DB
\c ecommerce_dev

-- Inspect total sales in Dev
SELECT SUM(total_amount)
FROM Orders;

-- Switch to Prod and run the same check
\c ecommerce_prod app_readonly
SELECT SUM(total_amount)
FROM Orders;

How to USE in PostgreSQL Syntax


-- psql meta-command to switch databases
\c database_name [user_name]   -- short form
\connect database_name [user_name]  -- long form

-- Example: switch to production DB as read-only role
\c ecommerce_prod app_readonly

Common Mistakes

Frequently Asked Questions (FAQs)

Can I switch databases inside a transaction?

No. \c immediately closes the current connection, discarding any open transaction.

Does \c preserve session settings?

It does not. GUCs like search_path revert to the defaults of the new database.

Is there a GUI equivalent?

Most GUI tools offer a database drop-down or a reconnect button that issues a new connection behind the scenes.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.