【问题标题】:Correct JDBC Connection URL for a PostgreSQL DB in a Docker Container更正 Docker 容器中 PostgreSQL 数据库的 JDBC 连接 URL
【发布时间】:2020-01-14 03:26:10
【问题描述】:

我正在使用 Gitpod 云 IDE,它使用 Docker 映像 来提供一个不错的 linux 环境VSCode IDE .

https://gitpod.io

可以在 Gitpod 中使用 Docker 文件 在环境中安装其他软件。因此,按照 Gitpod 文档,我安装了 PostgreSQL

https://medium.com/gitpod/bring-your-own-docker-image-to-gitpod-52db1aa861de

我现在可以使用此命令连接到 PostgreSQL。 请注意,我必须提供套接字文件夹才能使其正常工作

psql -h ~/pg/sockets postgres

我有一个 spring boot 应用程序,它应该连接到 PostgreSQL 数据库。连接 URL 取自环境变量。

export JDBC_DATABASE_URL=jdbc:postgresql://gitpod@127.0.0.1/postgres

这就是我的属性文件中的内容。

spring.datasource.url=${JDBC_DATABASE_URL}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true

这是我在 psql 上运行 \du 以列出用户时得到的。

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 gitpod    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

登录到这个 linux 环境的 Gitpod 用户名为 gitpod。 (我不知道密码)

当我运行命令 netstat -plunt | grep postgres 我得到以下输出

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      955/postgres 

但是当我运行我的应用程序时,它会抛出以下异常

Caused by: java.net.UnknownHostException: gitpod@127.0.0.1
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_202]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_202]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_202]
        at org.postgresql.core.PGStream.<init>(PGStream.java:75) ~[postgresql-42.2.6.jar!/:42.2.6]
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.6.jar!/:42.2.6]
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.6.jar!/:42.2.6]
        ... 56 common frames omitted

我认为应该是连接 URL 的问题。有人可以解释一下如何让这个工作吗?

非常感谢您的帮助。 谢谢。

【问题讨论】:

  • 不要把用户放在URL里,把它放在spring.datasource.username属性里。

标签: java postgresql spring-boot docker gitpod


【解决方案1】:

在 gitpod 社区的帮助下,我设法让它在 gitpod 中工作。

使用 gitpod 团队预先构建的 postgres docker 映像,如本文档中所述来安装 postgreSQL。 https://www.gitpod.io/blog/gitpodify/#postgresql

然后我把网址改成了下面的

export JDBC_DATABASE_URL=jdbc:postgresql://localhost:5432/postgres?user=gitpod

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2019-09-24
    • 2023-04-02
    相关资源
    最近更新 更多