Postgres以非预期的方式存储时间戳

我在我的应用程序中使用PostgreSQL的Knex查询生成器。 我正在尝试在UTC数据库中添加一个created_atupdated_at字段,并使用ISO8016格式的数据。 我想让我的数据如下所示:

 2017-04-20T16:33:56.774Z 

在我的Knex迁移中,我尝试使用.timestamps()方法手动创buildcreated_atupdated_at ,并使用.timestamp()方法.timestamp()命名。

当我种子我的数据库,并设置created_atupdated_at等于moment().utc().toISOString() ,但它存储在我的数据库中,如下所示:

 2017-04-20 11:20:00.851-05 

代码和数据库之间有更改数据的东西,我不知道它是Knex,Postgres节点库还是Postgres本身。

Postgres以内部格式存储时间戳,当您读取它时,会以您请求的格式显示它。

 knex_test=# update accounts set created_at = '2017-04-20T16:33:56.774Z'; UPDATE 47 knex_test=# select created_at from accounts where id = 3; created_at ---------------------------- 2017-04-20 19:33:56.774+03 (1 row) knex_test=# \d accounts Table "public.accounts" Column | Type | Modifiers ------------+--------------------------+------------------------------------------------------------- id | bigint | not null default nextval('test_table_one_id_seq'::regclass) last_name | character varying(255) | email | character varying(255) | logins | integer | default 1 about | text | created_at | timestamp with time zone | updated_at | timestamp with time zone | phone | character varying(255) | Indexes: "test_table_one_pkey" PRIMARY KEY, btree (id) "test_table_one_email_unique" UNIQUE CONSTRAINT, btree (email) "test_table_one_logins_index" btree (logins) knex_test=# 

您可以更改哪个时区postgres返回您的连接时间戳

 knex_test=# SET timezone = 'UTC'; SET knex_test=# select created_at from accounts where id = 3; created_at ---------------------------- 2017-04-20 16:33:56.774+00 (1 row) knex_test=# 

这里是如何完成与knex https://github.com/tgriesser/knex/issues/97

 var knex = Knex.initialize({ client: 'pg', connection: { host : '127.0.0.1', user : 'your_database_user', password : 'your_database_password', database : 'myapp_test', }, pool: { afterCreate: function(connection, callback) { connection.query('SET timezone = timezone;', function(err) { callback(err, connection); }); } } });