postgresql - Clojure 应用程序无法使用 SSL 连接到 Heroku Postgres

标签 postgresql heroku jdbc clojure

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

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)

这是我的项目.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,我根据 this page 的建议对其进行了编辑。 .

(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)

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

最佳答案

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

关于postgresql - Clojure 应用程序无法使用 SSL 连接到 Heroku Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18779571/

相关文章:

ruby - 为 Sinatra 应用程序连接到现有的 Heroku Postgres DB

java - jdbc如何插入mysql SET类型?

java - 如何在 Spring 中对两个查询使用相同的连接?

node.js - MIKRO-ORM:处理帖子计票的最佳方式是什么?

c - PostgreSQL通知处理

python - 无法从部署的 Heroku 应用程序中的 Django 数据库中检索

node.js - Heroku 上的 Node 调度(不是 Heroku 的 Scheduler 插件)

postgresql - plpgsql 函数中选择的数据类型并访问其字段

python - 针对 JSONField 的 Django 模型过滤器,其中键包含连字符/破折号

oracle - ORA-28040 : No matching authentication protocol