连接到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+",看不到其他会导致此问题的东西。在这一点上,我没有主意,无法弄清楚出了什么问题。

最佳答案

如果使用pgAdmin或psql,并使用用户localhost:6543和密码stranger登录到服务器stranger_user,数据库stranger_user,您会看到什么?

我希望您看到一个空的public模式。

运行create DATABASE stranger不会使其成为 Activity 数据库。

因此,运行create SCHEMA stranger将在postgres数据库中创建该架构。它还不会使新架构处于 Activity 状态。

因此,运行CREATE TABLE prescriptions将在public数据库的postgres模式中创建该表。

当然,在端口6543上提供的PostgreSQL数据库实例/群集中甚至都没有,因为您在端口7654上的其他实例/群集上完成了所有这些操作。

09-27 02:36