PostgreSQL ERROR: relation does not exist on INSERT Statement

PostgreSQL ERROR: relation does not exist on INSERT Statement

我正在尝试让我的代码在我的 table 中插入一行,称为 thoughtentries。它在 public 架构中。使用 psql 连接到我的数据库时,我能够 运行 ths 命令:

INSERT INTO thoughtentries VALUES('12/17/2016 14:10', 'hi');

第一列的类型为 character,长度为 17。第二列的类型为 text

当我的代码尝试使用上面相同的命令插入时,我在日志中收到错误:

ERROR: relation "thoughtentries" does not exist at character 13 STATEMENT: INSERT INTO thoughtentries VALUES('12/17/2016 14:11', 'hi');

我正在使用 pgpg-format 来格式化命令。这是我执行此操作的代码:

client.connect(function (err) {
  if (err) throw err
  app.listen(3000, function () {
    console.log('listening on 3000')
  })
  var textToDB = format('INSERT INTO thoughtentries VALUES(%s, %s);', timestamp, "'hi'")
  client.query(textToDB, function (err, result) {
    if (err) {
      console.log(err)
    }
    console.log(result)
    client.end(function (err) {
      if (err) throw err
    })
  })
})

我该如何解决这个问题?

您是否确认 table 实际上是在 public 架构中创建的?

SELECT *
FROM   information_schema.tables
WHERE  table_name = 'thoughtentries';

一旦你证实了这一点,我看到剩下两个可能的解释:

  1. 您错误地连接到另一个数据库。在同一会话中验证:

    select current_database();
    
  2. 您的 search_path 设置不包括 public 架构。如果是这种情况,您可以对 table 进行架构限定以修复:public.thoughtentries

    • How does the search_path influence identifier resolution and the "current schema"

旁白:将时间戳保存为数据类型 timestamp,而不是 character(17)
实际上,根本不要使用 character(n)

  • Any downsides of using data type "text" for storing strings?