java - 通过 JDBC 的 Postgres 连接 : org. postgresql.util.PSQLException: ERROR: relation "prescriptions"does not exist

标签 java database postgresql jdbc

我在连接到 dockerized postgres 实例时遇到了很多麻烦。我得到的错误看起来像这样:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: relation "prescriptions" does not exist
  Position: 29
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:321)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:313)

以下是重现问题的步骤。首先,我像这样设置数据库:

docker run -p 7654:5432 --name zoo_postgres -e POSTGRES_PASSWORD="stranger" -e POSTGRES_USER=stranger -d postgres

然后运行安装脚本,如下所示:

create DATABASE stranger;
create SCHEMA   stranger;

CREATE TABLE prescriptions (
  prescription_id BIGINT NOT NULL,
  PRIMARY KEY (prescription_id)
);

CREATE USER stranger_user WITH ENCRYPTED password 'stranger_user';

grant CONNECT on DATABASE stranger to stranger_user;
grant USAGE on SCHEMA stranger to stranger_user;

GRANT SELECT ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT INSERT ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT DELETE ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT UPDATE ON ALL TABLES IN SCHEMA stranger TO stranger_user;

-- I've tried several variations of the line below, without any effect
ALTER USER stranger_user SET SEARCH_PATH to stranger,stranger_user,public;

在 Java 端,代码非常简单,如下所示:

public static void main(String[] args) throws Exception {
    Class.forName("org.postgresql.Driver");
    Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:7654/stranger","stranger_user", "stranger_user"); // this succeeds (suggesting the connection string is correct)
    connection.setCatalog("stranger");
    connection.setSchema("stranger");
    Statement stmt = connection.createStatement();
    stmt.execute("select prescription_id from prescriptions");
    // code dies before getting to the close line.
    connection.close();
    System.out.println("Successfully connected using JDBC directly");
}

设置目录和模式无效,更改搜索路径也无效。一切都是小写的,所以我认为它与转义表名没有任何关系。我试过使用真实用户(并跳过用户帐户),省略数据库名称和其他几个技巧。我怀疑我连接到数据库的方式有问题,但我可以连接到使用 psql,如下所示:

% psql -h localhost -p 7654 -d postgres -U stranger_user

从这一点开始,我可以使用普通的 sql 访问表:

% psql -h localhost -p 7654 -d postgres -U stranger_user     
Password for user stranger_user: 
psql (9.4.6, server 9.5.2)
WARNING: psql major version 9.4, server major version 9.5.
         Some psql features might not work.
Type "help" for help.

postgres=> \d
              List of relations
  Schema  |     Name      | Type  |  Owner   
----------+---------------+-------+----------
 stranger | prescriptions | table | stranger
(1 row)

postgres=> select * from prescriptions;
 prescription_id 
-----------------
(0 rows)

postgres=> select * from stranger.prescriptions;
 prescription_id 
-----------------
(0 rows)

我正在使用最新版本的 postgres 驱动程序:"org.postgresql:postgresql:9.4+",并且看不到会导致此问题的任何其他内容。在这一点上,我没有想法,无法弄清楚出了什么问题。

最佳答案

您正在使用 psql 连接到端口 7654,然后在您的 JDBC URL 中连接到端口 6543。另外,您正在使用 连接到 postgres 数据库>psql,以及您的 JDBC URL 中的 stranger 数据库。

这些差异可能是您所看到的异常的来源吗?

https://jdbc.postgresql.org/documentation/94/connect.html

关于java - 通过 JDBC 的 Postgres 连接 : org. postgresql.util.PSQLException: ERROR: relation "prescriptions"does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37636007/

相关文章:

java - JLabel 在 Jpanel 上不可见

Java 和带有扫描器的枚举

php - 如何使用来自 xml-rpc 的数据更新数据库表?

MYSQL - 最佳数据结构

sql - 如何使用 date_trunc 函数进行双周分组

java - 在 Java 中将值附加到列表的元素

Java语法: Who can an interface be an object?

SQL Server - 在这里使用复合主键有什么好处?

postgresql - 未捕获 未捕获错误 : Unsupported operation: Socket constructor in dart with postgresql

django - 使用 Django 1.6 和 PostGreSQL 9.2 存储时区感知日期时间