![](/img/trans.png)
[英]org.postgresql.util.PSQLException: ERROR: relation “sequence” does not exist
[英]Postgres connectivity via JDBC: org.postgresql.util.PSQLException: ERROR: relation “prescriptions” does not exist
我在連接到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.此外,您將使用psql
連接到postgres
數據庫,並使用JDBC URL中的stranger
數據庫連接。
這些差異可能是您所看到的例外的來源嗎?
如果您使用pgAdmin或psql,並使用用戶stranger_user
和密碼stranger_user
登錄到服務器localhost:6543
,數據庫stranger
,您看到了什么?
我希望你看到一個空的public
架構。
運行create DATABASE stranger
並不會使它成為活動數據庫。
因此,運行create SCHEMA stranger
將在postgres
數據庫中創建該模式。 它也不會做出新的模式活躍。
因此,運行CREATE TABLE prescriptions
將在postgres
數據庫的public
模式中創建該表。
當然,甚至在端口6543上提供的PostgreSQL數據庫實例/集群中都沒有,因為您在端口7654上的不同實例/集群上完成了所有操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.