Skip to main content
0 reactions

How to Change a Setting for One Database in PostgreSQL

Featured card for How to Change a Setting for One Database in PostgreSQL, with a terminal showing ALTER DATABASE diary SET TimeZone TO Asia/Tokyo, then RESET TimeZone.
How to Change a Setting for One Database in PostgreSQL

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

SyntaxSQL
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 with SET.
  • 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:

TerminalBash
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:

postgres=#SQL
CREATE DATABASE diary;
OutputPlain text
CREATE DATABASE

Step 2: Give the Database Its Own Time Zone

postgres=#SQL
ALTER DATABASE diary SET TimeZone TO 'Asia/Tokyo';
OutputPlain text
ALTER DATABASE

Step 3: See It in a New Session

Connect to diary, which starts a new session, and show its time zone:

postgres=#Plain text
\c diary
OutputPlain text
You are now connected to database "diary" as user "postgres".
diary=#SQL
SHOW TimeZone;
OutputPlain text
  TimeZone
------------
 Asia/Tokyo
(1 row)

Other databases keep the server’s time zone:

diary=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
SHOW TimeZone;
OutputPlain text
   TimeZone
--------------
 Asia/Kolkata
(1 row)

Step 4: Remove the Database’s Value

postgres=#SQL
ALTER DATABASE diary RESET TimeZone;
OutputPlain text
ALTER DATABASE
postgres=#Plain text
\c diary
OutputPlain text
You are now connected to database "diary" as user "postgres".
diary=#SQL
SHOW TimeZone;
OutputPlain text
   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:

diary=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE diary;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
A misspelled setting nameERROR: unrecognized configuration parameter "timezon"Check the spelling. SHOW ALL lists every setting’s name.
Expecting open sessions to changeSessions 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

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