4

我一直在尝试在 Compojure 中制作一个基本的 Web 应用程序,托管在 Heroku 上。我一直在关注这个网站上的教程:

http://www.vijaykiran.com/2012/01/17/web-application-development-with-clojure-part-2/

并且已经在 Lobos 和 Korma 部分研磨了大约 2 天。我的应用程序现在可以连接到我的本地 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,但添加:ssl "true"到 config.clj 中的数据库映射。但是,现在我有一个新错误:

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)

如果您需要更多细节,请告诉我。

4

2 回答 2

4

我也无法对 heroku 实例上的 postgres 插件进行简单查询。具有类似设置的本地设置不适用于 heroku 设置。

我所做的是将:sslmode "require"键值对添加到我的 db-spec 映射中。

例如,这将是我的本地数据库规范。

(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 无法找到请求目标的有效证书路径。” 会发生。

于 2013-09-30T23:34:08.120 回答
0

目前最好的办法可能是简单地使用@#'parse-properties-url技巧来解决您在 clojure.java.jdbc 中需要的函数是私有的这一事实。

但是,此更改应该可以更轻松地在未来版本中打开 SSL:

https://github.com/clojure/java.jdbc/pull/35#issuecomment-32956962

于 2014-01-21T22:13:28.627 回答