How to USE Databases Like MariaDB in PostgreSQL

Galaxy Glossary

How do I perform MariaDB’s USE database task in PostgreSQL?

The USE command switches the active database; PostgreSQL accomplishes the same task with \c or SET search_path.

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

What does the USE command do in MariaDB?

USE database_name; tells MariaDB to make a different database the default for subsequent queries, so unqualified table names resolve inside that database.

How do I replicate USE in PostgreSQL?

PostgreSQL doesn’t implement USE. Instead, connect to another database with the meta-command \c or change your schema search path using SET search_path.

When should I run \c versus SET search_path?

Run \c when you must access an entirely different database cluster.Use SET search_path when you only need tables from another schema inside the same database.

What is the exact syntax?

See the Syntax section below for both approaches, including parameters.

Practical ecommerce example

Suppose you routinely jump between development and production databases that each contain Customers, Orders, Products, and OrderItems tables. You can switch with \c or set a schema path so queries stay concise.

Can I combine search_path with role-based permissions?

Yes.Grant your role usage on selected schemas, then set search_path at login for least-privilege access.

Best practices

Automate environment selection with connection strings rather than ad-hoc USE commands. For multi-schema databases, store SET search_path in a startup script or application pool settings.

Common pitfalls

Avoid assuming USE exists in PostgreSQL. Forgetting to adjust search_path may lead to “relation not found” errors if you omit schema prefixes.

Summary

MariaDB’s USE is conceptually split between \c and SET search_path in PostgreSQL.Pick the one matching your goal: new database or new schema context.

.

Why How to USE Databases Like MariaDB in PostgreSQL is important

How to USE Databases Like MariaDB in PostgreSQL Example Usage


-- Switch to the ecommerce_staging database
\c ecommerce_staging;

-- Now run a query without schema prefix
SELECT id, name, email
FROM Customers
WHERE created_at >= CURRENT_DATE - INTERVAL '30 days';

-- Stay in the same database but prioritise reporting schema
SET search_path TO reporting, public;

-- Retrieve total sales by day
SELECT order_date, SUM(total_amount) AS daily_total
FROM Orders
GROUP BY order_date
ORDER BY order_date DESC;

How to USE Databases Like MariaDB in PostgreSQL Syntax


-- Connect to another database (psql meta-command)
\c target_database [target_user]

-- Change default schema resolution order within current database
SET search_path TO schema1, schema2, public;

-- Example for ecommerce staging DB
\c ecommerce_staging;

-- Example limiting queries to sales schema first
SET search_path TO sales, public;

Common Mistakes

Frequently Asked Questions (FAQs)

Is \c available outside psql?

No. \c is a psql client command. Applications should open a new connection to the desired database instead.

Can I chain multiple SET commands?

Yes. You can set search_path per session, transaction, or even per user in postgresql.conf.

Will SET search_path impact performance?

Negligibly. PostgreSQL resolves schemas during planning; the overhead is minimal compared to query execution time.

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.