简体   繁体   中英

Sync engine without timezone or not in PostgreSQL?

First, I have read a lot about date types in Postgres using timezone or not. It seems that managing dates (timestamps) with timezone is the best to do in 99% of cases.

Currently I have built a sync engine using a Rails app set to UTC time by default (very important) and MariaDB managing dates (so UTC dates are written in the database but without seeing anything except the date: 2017-04-19 17:45:09 ). The devices that syncs to the server engine use local SQLite database with dates in UTC too. So for the same document, I can see the same date for a created_at field in the SQLite field and in the MariaDB field. It's working well.

Note that users never have to set a custom timezone in the server app and in the device apps. I want all UTC.

As I plan to migrate to PostgreSQL for many reasons, I'm quite sure I need to use Timestamp without timezone too, but I have some doubts after all readings.

What are your suggestions about this use case ?

EDIT :

So I made 3 tests using pgloader :

Test1 : MariaDB => PostgreSQL using computer default timezone

在此处输入图片说明

Dates are the same but as you can see, sometimes there is +01, sometimes +02 ???

Test2 : MariaDB => PostgreSQL using timezone after setting timezone to UTC in Postgres

在此处输入图片说明

Here there is +00, logical for UTC, but my dates were changed.

Test3 : MariaDB => PostgreSQL using timestamp WITHOUT timezone

在此处输入图片说明

All the same there.

"With Timezone" simply tells postgres to convert from local time to UTC before storing the timestamp, and to convert back to local time when querying. The underlying data is stored in UTC and no timezone information is stored in the actual data, it's simply a flag the DB uses to determine how to convert the data for human use.

If your client connection is setting the PG timezone to UTC upon DB connection, there will be pretty much no difference. Conventional wisdom says to always use timestamps with timezone, as it'll make queries in other systems (postgresql client) a lot more flexible and easy to use in the future.

try with one of these in your config file

 SET timezone= 'UTC' 
 SET client_timezone TO '+00:00'

my full settings were:

LOAD DATABASE
     FROM   mysql://usr@localhost/kk
     INTO   postgresql://usr:pwd@localhost/kk
     WITH include drop,
         create tables,
         create indexes,
         reset sequences,
         workers = 8, concurrency = 1,
         multiple readers per thread, rows per range = 50000
     SET PostgreSQL PARAMETERS
        maintenance_work_mem to '512MB',
        work_mem to '64MB',
        timezone to  'UTC'
     SET MySQL PARAMETERS
        net_read_timeout = '220',
        net_write_timeout = '220'
     SET timezone= 'UTC' 
     SET client_timezone TO '+00:00'
     ALTER SCHEMA 'kk' RENAME TO 'public'
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM