使用带有node-postgres的流的COPY进行Postgres日期

时间:2015-02-19 12:56:01

标签: node.js postgresql node-postgres

我在PostgreSQL中有以下表格:

CREATE TABLE history (
    request_date timestamp without time zone DEFAULT now() NOT NULL,
    user_id uuid,
    client_id character varying(255),
    resource character varying(255) NOT NULL,
    latency integer NOT NULL
);

我正在使用node-postgres执行COPY FROM任务。这就是我到目前为止所做的:

var pg = require('pg');
var copyFrom = require('pg-copy-streams').from;
var Readable = require('stream').Readable;

pg.connect(config.database.connection, function(err, client, done) {
  var stream = client.query(copyFrom('COPY history FROM STDIN WITH NULL as \'null\''));
  var rs = new Readable;

  rs.push(new Date() + '\tnull\tnull\t"' + req.originalUrl + '"\t' + responseTime);
  rs.push(null);
  rs.on('error', done);

  rs.pipe(stream).on('finish', done).on('error', function (err) {
    console.log(err);
    done();
  });
});

但我遇到日期问题(第一栏),我不知道Postgres需要什么格式,我收到以下错误:

{ [error: time zone "gmt-0300" not recognized]
  name: 'error',
  length: 175,
  severity: 'ERROR',
  code: '22023',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: 'COPY history, line 1, column request_date: "Thu Feb 19 2015 09:51:47 GMT-0300 (ART)"',
  file: 'datetime.c',
  line: '926',
  routine: 'DecodeDateTime' }

通过该日期的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

new Date().toUTCString()做了诀窍:

rs.push(new Date().toUTCString() + '\tnull\tnull\t"' + req.originalUrl + '"\t' + responseTime);

然后它起作用:)