连接到postgreSQL DB在Heroku与Korma(Clojure) [英] Trouble connecting to postgresql DB on Heroku with Korma (Clojure)

查看:328
本文介绍了连接到postgreSQL DB在Heroku与Korma(Clojure)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Heroku的配置设置中解析postgresql uri。但我似乎不能得到它的工作。任何帮助将非常感激,我可能错过了一些直接的东西。

I am parsing the postgresql uri in my config settings on Heroku. But I cannot seem to get it working. Any help would be greatly appreciated, I'm probably missing something straight forward.

这里是使用的代码。

(def dev-db-info
  {:db "dbname"
   :user "username"})

(defn parse-db-uri
  [uri]
  (drop 1 (split uri #"://|:|@|/")))

(defn create-map-from-uri
  [uri]
  (let [parsed (parse-db-uri uri)]
  (zipmap [:user :password :host :port :db] parsed)))

(defn db-info
  []
  (if production?
    (create-map-from-uri (System/getenv "DATABASE_URL"))
    dev-db-info))

(defdb connected-db
   (postgres (db-info)))

我从uri中检索的映射如下所示:

The map I retrieve from the uri looks like this:

{:db "dbname"
 :port "5662"
 :host "ec2-url.compute-1.amazonaws.com"
 :password "pwd"
 :user "username"}

我得到以下错误:

Connections could not be acquired from the underlying database!

编辑:

使用Korma,并切换到使用Clojure.JDBC 0.2.3,它支持connection-uri,因此ssl连接到db。 Korma目前不支持这一点。我将在Github上提交一个问题以允许此连接方法。

I have since given up on using Korma, and switched to using Clojure.JDBC 0.2.3 which supports "connection-uri" and therefore ssl connections to the db. Korma doesn't currently support this. I will file an issue on Github to allow this connection method.

推荐答案

编辑:
没有理由使用 [org.clojars.ccfontes / korma0.3.0-beta12-pgssl] 了。请阅读了解详情。此外,请忽略以下说明。

There's no reason to use [org.clojars.ccfontes/korma "0.3.0-beta12-pgssl"] anymore. Read this to know more about it. Also, please ignore the following instructions.

添加了 postgres SSL 支持。

project.clj 插入:
[org.clojars.ccfontes / korma0.3.0-beta12-pgssl]

定义连接到heroku上的postgres数据库:

Defining a connection to a postgres database on heroku:

(ns app.db
    (:require [clojure.java.jdbc :as sql]
              [korma.db :as db]
              [clojure.string :as string])
    (:import (java.net URI)))

    (defn set-app-pg-db! [mode]
      (let [db-uri (java.net.URI. (System/getenv "DATABASE_URL"))]
        (->> (string/split (.getUserInfo db-uri) #":")
          (#(identity {:db (last (string/split (System/getenv "DATABASE_URL") #"\/"))
                       :host (.getHost db-uri)
                       :port (.getPort db-uri)
                       :user (% 0)
                       :password (% 1)
                       :ssl true
                       :sslfactory (when (= mode :dev) "org.postgresql.ssl.NonValidatingFactory")}))
          (db/postgres)
          (db/defdb app-pg-db))))

修复使用 Tomcat JDBC连接池及其配置示例为连接池,因此它可能不太适合每个人的需要,加上这只是一个黑客。理想情况下,原始的Korma项目应该整合这些更改或其他可能的解决方案。

The fix uses Tomcat JDBC Connection Pool and their configuration sample for the connection pool, so it may not be well suited for everyone's needs, plus this is only a hack. Ideally the original Korma project should integrate these changes or other possible solution.

感谢其他人的反馈,因为它只在我自己的项目中测试。谢谢。

Would appreciate some feedback from other people since it was only tested in my own project. Thanks.

这篇关于连接到postgreSQL DB在Heroku与Korma(Clojure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆