【问题标题】:Postgres date using COPY from stream with node-postgresPostgres 日期使用来自带有 node-postgres 的流中的 COPY
【发布时间】:2015-02-19 21:01:12
【问题描述】:

我在 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' }

什么应该是正确的方式来度过那个日期?

【问题讨论】:

  • 我认为 copy 需要一个 ISO 格式的时间戳 (2015-02-19 ...)
  • 是的,这就是问题所在。我用.toUTCString()解决了这个问题

标签: node.js postgresql node-postgres


【解决方案1】:

new Date().toUTCString() 成功了:

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

然后它就起作用了:)

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 2018-01-11
    • 2020-11-05
    • 1970-01-01
    • 2013-12-06
    • 2016-03-18
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多