Search code examples
node.jspostgresqlnode-postgres

Insert unix timestamp to postgres timestamp column (node-postgres)


I'm using node-postgres module and node 13.5. I'm trying to insert a unix timestamp to postgres timestmap column and it seems to work in dbeaver but when I run the code it doesn't work.

CREATE TABLE public.test (
    column1 timestamp NULL
);

In dbeaver when I run my insert:

insert into test (column1 ) values ( $1 ) 

It opens up a dialog I type in my param as: to_timestamp(1) I hit ok and it inserts to my table without problem.

But when I use this code:

pool.query("INSERT INTO test (column1) VALUES ($1)", ["to_timestamp(1)"]);

I get an error:

error: invalid input syntax for type timestamp: "to_timestamp(1)"

the query method is the one from the module.

Also if I run like this:

pool.query("INSERT INTO test (column1) VALUES (to_timestamp(1))", []);

It works.

It seems like the nodejs driver doing something different from the dbeaver driver. Am I doing this wrong? Is there a way to to approach this problem? I'd like to use prepared statement if possible.

The things I can't change:

  • my data is in unix timestamp
  • the column must have timestamp type

Any help appreciated.


Solution

  • My college helped me:

    pool.query("INSERT INTO test (column1) VALUES to_timestamp($1)", ["1"]);
    

    This is the working solution!