2017-02-18 87 views
0

我想查詢我在本地機器上設置的postgres數據庫。查詢Postgres數據庫 - Clojure

我創建了一個文件

(ns website.db 
    (:require [clojure.java.jdbc :as jdbc])) 

(def database 
    {:classname "com.postgres.jdbc.Driver" 
    :subprotocol "postgres" 
    :subname "mydb" ; In the guide this was //127.0.0.1:3306/mydb. Is the first part my computer IP address? 
    :user "admin" 
    :password "secret"}) 

如果我再嘗試查詢使用數據庫(在REPL)

(jdbc/query database ["SELECT * FROM table"]) 

我得到的錯誤ConnectException Connection refused java.net.PlainSocketImpl.socketConnect (PlainSocketImpl.java:-2)

注意我所期望的數據庫爲空,因爲我沒有在數據庫中輸入任何信息

定義數據庫時我犯了什麼錯誤?

要插入用戶這是正確的:

(defn register-user! [db name] 
    (jdbc/insert! db :user {:name name})) 
+0

據例如在GitHub上[github上]官方回購(https://github.com/clojure/java .dbdb#example-usage)你的數據庫定義應該像'(def pg-db {:dbtype「postgresql」 :dbname「mypgdatabase」 :host「mydb.server.com」 :user「myuser」 :密碼「secret」 :ssl true :sslfactory「org.postgresql.ssl.NonValidatingFactory」})' – wwajerowicz

回答

2

clojure.java.jdbc文檔站點並不包括所有的可能性,並沒有太多細節。

我想指出你the doc of get-connection功能是相當廣泛:

(...) db-spec is usually a map containing connection 
    parameters but can also be a URI or a String. The various possibilities are described 
    below: 
    DriverManager (preferred): 
    :dbtype  (required) a String, the type of the database (the jdbc subprotocol) 
    :dbname  (required) a String, the name of the database 
    :host  (optional) a String, the host name/IP of the database 
          (defaults to 127.0.0.1) 
    :port  (optional) a Long, the port of the database 
          (defaults to 3306 for mysql, 1433 for mssql/jtds, else nil) 
    (others)  (optional) passed to the driver as properties. 
    Raw: 
    :connection-uri (required) a String 
       Passed directly to DriverManager/getConnection 
    Other formats accepted: 
    Existing Connection: 
    :connection (required) an existing open connection that can be used 
       but cannot be closed (only the parent connection can be closed) 
    DriverManager (alternative/legacy style): 
    :subprotocol (required) a String, the jdbc subprotocol 
    :subname  (required) a String, the jdbc subname 
    :classname (optional) a String, the jdbc driver class name 
    (others)  (optional) passed to the driver as properties. 
    Factory: 
    :factory  (required) a function of one argument, a map of params 
    (others)  (optional) passed to the factory function in a map 
    DataSource: 
    :datasource (required) a javax.sql.DataSource 
    :username (optional) a String 
    :user  (optional) a String - an alternate alias for :username 
          (added after 0.3.0-beta2 for consistency JDBC-74) 
    :password (optional) a String, required if :username is supplied 
    JNDI: 
    :name  (required) a String or javax.naming.Name 
    :environment (optional) a java.util.Map 
    java.net.URI: 
    Parsed JDBC connection string (see java.lang.String format next) 
    java.lang.String: 
    subprotocol://user:[email protected]:post/subname 
       An optional prefix of jdbc: is allowed." 

最新clojure.java.jdbc版本還包括Clojure specs for db-spec map

你的情況可能是:

{:dbtype "postgresql") 
:dbname "mydb" 
:host "127.0.0.1" 
:port 5432 
:user "admin" 
:password "secret"} 
0

你的配置是好的,對我的作品時,我用我自己的本地的Postgres數據庫,但在對方的回答給出的配置是清潔IMO。

做這種事情時,最好的方法是能夠在使用第三方客戶端與數據庫交談之前,以編程方式嘗試這樣做。

假設您使用* nix操作系統,您應該可以使用psql來管理數據庫。創建您的角色,分配密碼,創建數據庫,創建表格,並在表格中插入一行或兩行。在postgres配置中,關閉ssl,使其保持簡單,直到它能夠正常工作,然後在需要時添加它。

最難的部分是簡單地得到postgres配置正確。一旦你已經驗證了訪問和使用psql,那麼clojure部分是微不足道的。

FWIW,工作,跟上時代的你應該有依賴關係的版本是:

[org.clojure/java.jdbc "0.7.0-alpha1"] 
[postgresql/postgresql "9.3-1102.jdbc41"] 
+0

我認爲@Josh是在正確的軌道上。默認情況下,在許多* nix平臺上,數據庫設置爲僅允許本地域socketes而不允許tcp。檢查你的postgresql配置,並確保你可以使用用戶名和密碼連接到數據庫。 –