2017-02-18 4 views
0

自分のマシンにローカルに設定したpostgresデータベースを照会しようとしています。Postgresデータベースを照会する - Clojure

私はその後、(REPLで)使用してデータベースを照会しようとした場合、私は、ファイル

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

を作成している

(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の](https://github.com/clojure/java .jdbc#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機能にあなたを指すようになります。最新clojure.java.jdbc版もClojure specs for db-spec mapが含ま

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

あなたのケースでは、それは次のようになります。

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

あなたの構成は結構です、と私は自分のローカルPostgresのDBを使用するときに、他の回答に与えられた設定は、IMOクリーナーですが、私のために動作します。

このようなことを行う際の最善のアプローチは、プログラマチックに行う前に、サードパーティのクライアントを使用してデータベースに話をすることです。

* nix OSを使用していると仮定すると、psqlを使用してデータベースを管理できるはずです。ロールを作成し、パスワードを割り当て、データベースを作成し、テーブルを作成し、テーブルに1行または2行を挿入します。 postgresの設定では、sslをオフにして、動作させるまでシンプルにして、必要に応じて追加します。

最も難しい部分は、単純にpostgresの設定を正しいものにすることです。 psqlでアクセスと使用を確認したら、クロージャーの部分は簡単です。

FWIW、仕事とあなたが持つべき依存関係の最新バージョンは、次のとおりですgithubの上の公式レポの例によれば

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

私は@Joshが正しい道にあると思います。デフォルトでは、多くの* nixプラットフォームで、データベースはローカルドメインソケットのみを許可し、tcpは許可しないように設定されています。 PostgreSQLの設定を確認し、ユーザ名とパスワードを使用してデータベースに接続できることを確認してください。 –

関連する問題