Clojure应用无法通过SSL连接到Heroku的Postgres

4
我一直在尝试使用Compojure创建一个基本的Web应用程序并将其托管在Heroku上。 我一直在遵循此网站上的教程:http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/,并且已经花了大约两天时间来学习Lobos和Korma部分。 我的应用程序现在可以连接到我的本地Postgres服务器,但是当我尝试推送到Heroku或连接到我的Heroku Postgres数据库时,我会收到以下错误:
PSQLException FATAL: no pg_hba.conf entry for host "the IP", user "the username", database "the dbname", SSL off  org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication (ConnectionFactoryImpl.java:291)

这是我的project.clj文件:

(defproject portfolio "1.0.0-SNAPSHOT"
  :description "My personal portfolio"
  :url "the URL"
  :license {:name "FIXME: choose"
            :url "http://example.com/FIXME"}
  :dependencies [[compojure "1.1.1"]
                 [ring/ring-jetty-adapter "1.1.0"]
                 [ring/ring-devel "1.1.0"]
                 [ring-basic-authentication "1.0.1"]
                 [environ "0.4.0"]
                 [com.cemerick/drawbridge "0.0.6"]
                 [hiccup "1.0.4"]
                 [lobos "1.0.0-beta1"]
                 [korma "0.3.0-RC5"]
                 [org.clojure/java.jdbc "0.2.3"]
                 [postgresql "9.1-901.jdbc4"]
                 [clj-yaml "0.3.1"]
                 [http.async.client "0.5.2"]
                 [clj-bonecp-url "0.1.0"]
                 [org.slf4j/slf4j-nop "1.7.2"]
                 [org.clojure/clojure "1.5.1"]]
  :min-lein-version "2.0.0"
  :plugins [[environ/environ.lein "0.2.1"]]
  :hooks [environ.leiningen.hooks]
  :profiles {:production {:env {:production true}}})

我正在使用lobos(https://github.com/budu/lobos)进行数据迁移。我遵循了GitHub页面上的建议,并制作了一个config.clj文件,然后根据这篇文章的建议进行了编辑。

(ns lobos.config
  (:refer-clojure :exclude [replace reverse])
  (:use [clojure.string :as str]
        lobos.connectivity)
  (:import (java.net URI)))

(defn heroku-db
  "Generate the db map according to Heroku environment when available."
  []
  (when (System/getenv "DATABASE_URL")
    (let [url (URI. (System/getenv "DATABASE_URL"))
          host (.getHost url)
          port (if (pos? (.getPort url)) (.getPort url) 5432)
          path (.getPath url)]
      (merge
       {:subname (str "//" host ":" port path)}
       (when-let [user-info (.getUserInfo url)]
         {:user (first (str/split user-info #":"))
          :password (second (str/split user-info #":"))})))))

(def db
  (merge {:classname "org.postgresql.Driver"
          :subprotocol "postgresql"
          :subname "//localhost:5432/blogdb"}
         (heroku-db)))

(defn open-global-when-necessary
  "Open a global connection only when necessary, that is, when no previous
  connection exist or when db-spec is different to the current global
  connection."
  [db-spec]
  ;; If the connection credentials has changed, close the connection.
  (when (and (@lobos.connectivity/global-connections :default-connection)
             (not= (:db-spec (@lobos.connectivity/global-connections :default-connection)) db-spec))
    (lobos.connectivity/close-global))
  ;; Open a new connection or return the existing one.
  (if (nil? (@lobos.connectivity/global-connections :default-connection))
    ((lobos.connectivity/open-global db-spec) :default-connection)
    (@lobos.connectivity/global-connections :default-connection)))


(open-global-when-necessary db)

这给了我上面提到的错误。

我设法找出如何启用SSL,只需在config.clj中的db映射中添加:ssl“true”。然而,现在我有一个新错误:

SunCertPathBuilderException unable to find valid certification path to requested target.

当我尝试将代码推送到Heroku时,无论SSL是否开启,都会出现以下错误:
Exception in thread "main" org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections., compiling:(config.clj:44:1)

如果您需要更多具体信息,请让我知道。

请参考以下类似问题:[在Heroku上使用Korma(Clojure)连接postgresql数据库时出现问题] (https://dev59.com/kWrWa4cB1Zd3GeqP9lJL) - Brad Koch
2个回答

4
我在Heroku实例中使用Postgres addon进行简单查询时遇到了麻烦,因为本地设置与Heroku设置类似,但无法正常工作。
我的解决方法是在我的数据库规范映射中添加:sslmode "require"键值对。
例如,以下是我本地DB规范的示例,可以正常工作。
(def db-str {:classname "org.postgresql.Driver"
         :subprotocol "postgresql"
         :subname "//localhost:5432/testdb"
         :user "postgres"
         :password "password"})

这是我的Heroku数据库规格。
(def db-str {:classname "org.postgresql.Driver"
         :subprotocol "postgresql"
         :subname "//remotehost:5432/testdb"
         :user "postgres"
         :password "password"
         :sslmode "require"})

请注意 :sslmode 键和其对应的值。 如果没有 :sslmode 键-值对,将出现 "SunCertPathBuilderException 无法找到请求目标的有效验证路径。"

我正在使用连接字符串,如何在其中要求 sslmode? - Justin Thomas

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接