Explain Codes LogoExplain Codes Logo

Permanently Set Postgresql Schema Path

sql
database
postgresql
schema
Alex KataevbyAlex Kataev·Jan 4, 2025
TLDR

Permanently modify the PostgreSQL search_path with ALTER commands:

ALTER DATABASE db_name SET search_path TO schema_name; ALTER ROLE role_name SET search_path TO schema_name;

Substitute db_name for your database name, role_name for your user/role name, and schema_name for your desired schema. Every new session defaults to your set schema using this method.

User and Database Specific Schema Configuration

PostgreSQL allows you to set your search_path at specific levels. These instructions inform PostgreSQL where to look for tables and other objects.

Setting for a Specific User

For a certain user (role), setting the search_path applies across all databases the user can access:

ALTER ROLE very_cool_user SET search_path TO very_cool_schema, public;

Note: User's charisma attribute not increased by setting to very_cool_user 😉

Setting for a Particular Database

Alternatively, the search_path can be configured to apply to anyone connecting to a certain database:

ALTER DATABASE very_important_db SET search_path TO very_important_schema, public;

By setting search_path, database does not actually become more important 👑

Setting for a User within a Specific Database

For a fine-grain control, you can alter the search_path for a user within a certain database:

ALTER ROLE lucky_user IN DATABASE jackpot_db SET search_path TO jackpot_schema, public;

No, this does not increase your chances of winning a lottery. Just schema luck! 🍀

Lookout for Lookup Order, Seekers!

The order written in search_path determines how PostgreSQL resolves object names.

The Order Matters in Search Path

SET search_path TO first_place, second_place, public;

This isn't a race, but first_place gets searched first!

Quotes for Schema Names: For the Special Ones

Schema names need double quotes if they contain special characters or are case-sensitive:

SET search_path TO "MySpecialSchema", public;

No autographs please, "MySpecialSchema" has entered the building! 🎥

Setting a System-wide Default Schema

You might need a common schema default across systems for easier management. But remember to adjust it to suit each application's needs.

ALTER ROLE Postgres SET search_path TO default_schema, public;

No Postgres, the "default_schema" isn't boring. It's... default 🗂️

Spelunking Deeper into Schema Path Choices

Sharpen your schema path management skills even more by following these advanced insights.

The Power of PostgreSQL Configuration

The postgresql.conf file allows setting a system-wide default search path:

search_path = '"$user", public'

This search_path isn't a road, but all users will take its route. 🛣️

Environmental Variables to the Rescue!

Environmental variables provide an alternative for your temporary or session-based default settings:

PGOPTIONS='--search_path=myschema' psql

Want a different path? There's an environmental variable for that! 🌍

Search Path Sequence - Choose Wise & Precise

Stick to the correct sequence while setting multiple default schemas in search_path. It could make or break your query results:

SET search_path = schema1, schema2, public;

It's the schema1, schema2, public conga line! 🕺💃🕺