16

We use the latest MySQL server on the AWS RDS instance and we have configured to run it on US-East data center. We are assuming that any new Date() or Time.now() invocation will store the date in the timezone in which the database server is running.

Is there a way to point my AWS RDS instance running on US-East to point to the PST timezone, so that any persistence of date will store the values in PST instead of EST. (i.e. If store an object around 10 AM EST, the db column should reflect 7 AM EST instead).

0

1 Answer 1

38

Before the rest of my answer, I'd just like to recommend right now that if you have any option to change your application to use UTC it will save you a lot of grief now and in the future.

However given the context of your question, I'm assuming that this isn't an option and that you're adapting a system designed to use MySQL in a traditional server environment where you can just change the timezone, and that there's code logic that expects this timezone and can't be easily adapted to use UTC.

But if you really need it to store as PST, read on.


You're correct that mySql will use the server's timezone for timestamp storage by default, however your assumption that RDS instances have their timezones set based on the AWS region in which they are launched is incorrect - all RDS instances are launched with their timezone set as UTC, and this configuration can't be changed:

The time zone is currently not modifiable. There is indeed a Parameter value in rds-describe-db-parameters called "default_time_zone" but it's marked as not modifiable.

So your only option is to set the timezone on each connection your application makes to the database instance to PST. You can use the SET SESSION time_zone = 'PST' query to execute in every connection an application makes by following the two steps found here:

  1. Create the following stored procedure (UTC-8 is PST):

    DELIMITER |  
    CREATE PROCEDURE mysql.store_time_zone ()  
       IF NOT (POSITION('rdsadmin@' IN CURRENT_USER()) = 1) THEN     
           SET SESSION time_zone = '-8:00';  
       END IF 
    |
    DELIMITER ;
    
  2. Connect to your instance, and run the following command:

    $ rds-modify-db-parameter-group PARAMGROUP --parameters "name=init_connect, value='CALL mysql.store_time_zone', method=immediate"
    
  3. You may need to grant EXECUTE permissions to the users that will be connecting to the database, otherwise you may get a connection error:

    GRANT EXECUTE ON PROCEDURE mysql.store_time_zone TO 'user'@'host';
    

Now every query executed against your RDS instance by any client should use PST without modifying any application logic and without needing to update any previously-stored timestamps in the database.

9
  • 14
    A better solution would be to set your named timezone to handle daylight savings. I suggest changing the line to: SET SESSION time_zone ='US/Pacific'; May 15, 2013 at 19:07
  • I want to put stress on previous comment, named timezone really saves pain in daylight savings, be aware of GMT too, as it does not hold daylight savings offset
    – Zavael
    Apr 9, 2015 at 12:22
  • @ThomasPaine My only fear of this solution is that, it seems every new user that is added to this database will have to have access to this database function or it will not work, correct? What about other databases and their users. Can I create a database with this stored procedure and somehow make sure everyone can access it by default?
    – Neo
    Aug 6, 2015 at 17:29
  • @Ryan Weir, I was only able to get this to work with using user() instead of current_user(), do you know why your solution seems to be working for everyone but me with current_user(); the strange thing is current_user() returns the same thing from command line just in the context of init_connect it actually returns the initial master user you create with RDS regardless of the user that is connecting to MySQL. I verified this by changing the procedure to insert a record with the value of current_user();
    – Neo
    Aug 13, 2015 at 3:12
  • 11
    Please note that this accepted answer is not correct any more. You can now set timezone values for databases in RDS: aws.amazon.com/de/premiumsupport/knowledge-center/…
    – janpio
    Feb 16, 2016 at 19:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.