node.js - 使用来自流的 COPY 和 node-postgres 的 Postgres 日期

标签 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' }

传递该日期的正确方法应该是什么?

最佳答案

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

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

然后就可以了:)

关于node.js - 使用来自流的 COPY 和 node-postgres 的 Postgres 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28607007/

相关文章:

node.js - 从 Cloudinary 切换到 S3 - 无法上传

node.js - 快速/连接 : is it possible to use the static middleware several times?

sql - 地址/邮政编码和电话的部分/匹配的复杂查询帮助

postgresql - 在 github 操作中使用 psql

sql - Postgres 似乎忽略了我的默认排序规则

node.js - 用于 PostgreSQL 连接 nodejs 的 SSL

node.js - AWS Lambda上的 Node 函数无法使用node-pg连接到数据库

node.js - 使用 grunt、bower、node 模块部署到 heroku

javascript - 在 node.js 中导入 sql 文件并针对 PostgreSQL 执行

ios - 创建一个airplay服务器nodejs