Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

890
Views
Knex con Postgres almacenando la marca de tiempo de manera no deseada

Estoy usando el generador de consultas Knex con Postgres en mi aplicación. Estoy tratando de agregar campos created_at y updated_at en mi base de datos con datos con formato ISO8016 en hora UTC. Estoy tratando de que mis datos se vean así:

 2017-04-20T16:33:56.774Z

En mis migraciones Knex, he intentado usar el método .timestamps() creando tanto created_at como updated_at manualmente con el método .timestamp() y nombrándolos yo mismo.

Cuando sembré mi base de datos y configuré created_at y updated_at igual a moment().utc().toISOString() , pero se almacena en mi base de datos así:

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

Hay algo entre el código y la base de datos que cambia los datos y no sé si es Knex, la biblioteca de nodos de Postgres o el propio Postgres.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Postgres almacena la marca de tiempo en formato interno y cuando lo lee lo muestra en el formato que lo solicita.

 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=#

Puede cambiar en qué zona horaria postgres devuelve las marcas de tiempo para su conexión con

 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=#

Y así es como se hace con 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); }); } } });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!