ALTER DATABASE … SET gives one PostgreSQL database its own value for a setting, such as its time zone. Every new session in that database then starts with that value, while other databases keep the server’s. Here’s how to set it, see it work, and remove it again.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
ALTER DATABASE database_name SET setting_name TO value;
ALTER DATABASE database_name RESET setting_name;
database_name: the database that gets its own value.setting_name TO value: the setting and its new value, as withSET.RESET: removes the database’s own value, so new sessions use the server’s again.- Only the database’s owner or a superuser can run it.
See ALTER DATABASE (opens in a new tab) in the PostgreSQL documentation.
The Example
A diary database should show times in Tokyo time, while the rest of the server keeps its own time zone.
Step 1: Connect and Create a Test Database
Open Terminal and connect to your PostgreSQL server with psql as the postgres user:
psql -U postgres
Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example:
CREATE DATABASE diary;
CREATE DATABASE
Step 2: Give the Database Its Own Time Zone
ALTER DATABASE diary SET TimeZone TO 'Asia/Tokyo';
ALTER DATABASE
Step 3: See It in a New Session
Connect to diary, which starts a new session, and show its time zone:
\c diary
You are now connected to database "diary" as user "postgres".
SHOW TimeZone;
TimeZone
------------
Asia/Tokyo
(1 row)
Other databases keep the server’s time zone:
\c postgres
You are now connected to database "postgres" as user "postgres".
SHOW TimeZone;
TimeZone
--------------
Asia/Kolkata
(1 row)
Step 4: Remove the Database’s Value
ALTER DATABASE diary RESET TimeZone;
ALTER DATABASE
\c diary
You are now connected to database "diary" as user "postgres".
SHOW TimeZone;
TimeZone
--------------
Asia/Kolkata
(1 row)
New sessions in diary use the server’s time zone again.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE diary;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A misspelled setting name | ERROR: unrecognized configuration parameter "timezon" | Check the spelling. SHOW ALL lists every setting’s name. |
| Expecting open sessions to change | Sessions that were already connected keep their old value. | Connect again, as with \c diary in Step 3. |
FAQ
How is this different from SET?
SET changes a setting for your current session only. ALTER DATABASE … SET changes the starting value of every new session in one database.
Conclusion
ALTER DATABASE … SET gives one database its own value for a setting, from the next session on, and RESET removes it.
Additional Resources
- PostgreSQL 19 documentation: ALTER DATABASE (opens in a new tab)
- PostgreSQL 19 documentation: Setting Parameters (opens in a new tab)
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments