查询Postgres数据库 - Clojure

时间:2017-02-18 15:19:36

标签: postgresql jdbc clojure

我正在尝试查询我在本机上本地设置的postgres数据库。

我创建了一个文件

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

2 个答案:

答案 0 :(得分: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:password@host: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"}

答案 1 :(得分: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"]