The Equivalent of "USE database_name" in PostgreSQL
In PostgreSQL, switching between databases within a single session is different from other database systems like MySQL, which uses the command USE database_name. PostgreSQL does not support this command due to its design. Instead, each database connection in PostgreSQL is tied to a specific database, meaning you need to reconnect to switch databases.
To "switch" databases in PostgreSQL, you must either:
Syntax to Connect to a Database in PostgreSQL
1. Reconnecting from the Command Line
To connect to a PostgreSQL database, use the following command directly from your terminal:
# Connect to a PostgreSQL database from the terminal psql -U username -d database_name
Explanation:
2. Switching Databases with \c in psql
Within the psql environment, use the \c command to change to another database:
-- Switch to another database within psql \c database_name username
Explanation:
Example and Explanation
1. Connecting to a Database from the Command Line
Code:
# Connect to the "sales" database as user "admin"
psql -U admin -d sales
Explanation:
2. Switching Databases Within psql
Code:
-- Inside psql, switch to the "inventory" database
\c inventory admin
Explanation:
Important Notes:
All PostgreSQL Questions, Answers, and Code Snippets Collection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4