You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ci-skip] Rails Guides: PostgreSQL timestamp with time zone
This guide addition documents how to configure your Rails application to
use the data type "timestamp with time zone" (timezone-aware timestamp),
instead of the default type, "timestamp without time zone"
Support for this data type was added earlier in Active Record, but
requires a configuration change in order to use, and from these PR
comments, where, why, and how to perform this configuration change
wasn't added to the Rails Guides Active Record PostgreSQL documentation
from what I could tell
Related PR: rails#41084
Guide page: https://guides.rubyonrails.org/active_record_postgresql.html
PostgreSQL best practices:
https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timestamp_.28without_time_zone.29
Rails migrations with timestamps store the time a model was created or updated. By default and for legacy reasons, the columns use the `timestamp without time zone` data type.
524
+
525
+
```ruby
526
+
# db/migrate/20241220144913_create_devices.rb
527
+
create_table :post, id::uuiddo |t|
528
+
t.datetime :published_at
529
+
# By default, Active Record will set the data type of this column to `timestamp without time zone`.
530
+
end
531
+
```
532
+
533
+
While this works ok, [PostgreSQL best practices](https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timestamp_.28without_time_zone.29) recommend that `timestamp with time zone` is used instead for timezone-aware timestamps.
534
+
This must be configured before it can be used for new migrations.
535
+
536
+
To configure `timestamp with time zone` as your new timestamp default data type, place the following configuration in the `config/application.rb` file.
537
+
538
+
```ruby
539
+
# config/application.rb
540
+
ActiveSupport.on_load(:active_record_postgresqladapter) do
541
+
self.datetime_type =:timestamptz
542
+
end
543
+
```
544
+
545
+
With that configuration in place, generate and apply new migrations, then verify their timestamps use the `timestamp with time zone` data type.
0 commit comments