How to change timezone in PostgreSQL?

by giovanny.lueilwitz , in category: PHP Databases , 2 years ago

How to change timezone in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by jasen_gottlieb , a year ago

@giovanny.lueilwitz 

To change the time zone in PostgreSQL, you can use the SET TIME ZONE command. The syntax is as follows:

1
SET TIME ZONE interval;


Where interval is the time offset from UTC. You can specify the time offset as a string in the format '+HH:MM' or '-HH:MM', or as an interval value such as INTERVAL '4 hours'.


For example, to set the time zone to Pacific Standard Time (PST), which is UTC-8, you can use the following command:

1
SET TIME ZONE '-08:00';


Alternatively, you can specify the time offset as an interval value:

1
SET TIME ZONE INTERVAL '-8 hours';


Keep in mind that the time zone setting is only used to display data in the desired time zone. It does not actually change the underlying data stored in the database.


You can also set the time zone at the database level by modifying the timezone parameter in the postgresql.conf configuration file and then restarting the PostgreSQL server. This will set the default time zone for all new connections to the database.

1
2
# Set the default time zone to Pacific Standard Time (PST)
timezone = 'PST'


Alternatively, you can set the time zone for a particular session by setting the TIMEZONE environment variable before starting the psql client. This will override the default time zone setting for the current session.

1
2
3
4
5
# Set the time zone to Pacific Standard Time (PST) for the current session
export TIMEZONE='PST'

# Start the psql client
psql


by tressie.damore , 9 months ago

@giovanny.lueilwitz 

To change the timezone in PostgreSQL, you can follow these steps:

  1. Connect to the PostgreSQL database using an administrative tool such as pgAdmin or by using the psql command-line interface.
  2. Run the following SQL command to see the current timezone setting: SELECT current_setting('timezone');
  3. To change the timezone, you can use the SET command with the timezone parameter. For example, to set the timezone to 'UTC', run the following command: SET timezone='UTC'; Note: Replace 'UTC' with the desired timezone you want to set.
  4. After running the SET command, verify the change by running the SELECT command again: SELECT current_setting('timezone'); It should display the updated timezone.
  5. If you want to set the timezone globally for all future sessions, you can modify the postgresql.conf file. Locate the postgresql.conf file. The exact location depends on the installation and operating system but can typically be found in the data directory of your PostgreSQL installation. Open the postgresql.conf file in a text editor. Find the timezone configuration parameter and update it with the desired timezone. For example: timezone = 'UTC' Save the changes to the postgresql.conf file.
  6. Restart the PostgreSQL server for the changes to take effect.


By following these steps, you can change the timezone in PostgreSQL either temporarily for the current session or globally for all future sessions.