[Ruby] Unify the Rails app time zone to Japan time

less than 1 minute read

There are four time zones to consider:

  • Ruby process -Affects Time.now etc.
  • config.time_zone -Affects Time.zone.now etc.
  • config.active_record.default_timezone -What time zone to interpret the time received from the DB
  • DB -Affects NOW() etc. of SQL statement

Ruby process

Development environment (Docker)

Support by adding ENV TZ Asia/Tokyo to Dockerfile

Production environment

Support by setting TZ = Asia/Tokyo in the environment variable




config.time_zone ='Tokyo'




config.active_record.default_timezone = :local


Execute the following query by connecting to each DB of development environment and production environment

ALTER DATABASE db_name SET timezone TO'Asia/Tokyo';

Since it is the writing method for Postgres, the writing method may be different depending on the type of DB. It is reflected when reconnecting to DB. When you execute rails:db:migrate, the time zone will be restored, but it’s good because you rarely write SQL statements in Rails in the first place. There seems to be a way to set an environment variable in docker-compose.yml, but it didn’t work.