查询Postgres数据库 - Clojure

3

我正在尝试查询我在本地机器上设置的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 连接被拒绝 java.net.PlainSocketImpl.socketConnect (PlainSocketImpl.java:-2)

请注意,由于我没有向数据库中输入任何信息,因此我希望数据库为空

在定义我的数据库时,我犯了什么错误?

要插入用户,是否正确:

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

根据 Github 上官方仓库的示例 github,你的数据库定义应该类似于 (def pg-db {:dbtype "postgresql" :dbname "mypgdatabase" :host "mydb.server.com" :user "myuser" :password "secret" :ssl true :sslfactory "org.postgresql.ssl.NonValidatingFactory"}) - wwajerowicz
2个回答

2

clojure.java.jdbc文档网站并未涵盖所有可能性,也没有太多细节。

我建议你查看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版本还包括用于db-spec映射的Clojure规范

在您的情况下,它可能是:

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

1

您的配置很好,在我使用自己的本地Postgres数据库时可以正常工作,尽管其他答案中给出的配置在我看来更加简洁。

在进行这种操作时,最好的方法是先使用第三方客户端与数据库进行交互,然后再尝试编程实现。

假设您正在使用*nix操作系统,您应该能够使用psql管理数据库。创建您的角色,分配密码,创建数据库,创建表格并将一两行数据插入表格中。在postgres配置中,关闭ssl以保持简单,直到您使其正常工作,然后再添加它(如果需要)。

最难的部分就是正确获取postgres配置。一旦您已经通过psql验证了访问和使用权限,那么clojure部分就非常简单了。

顺便说一下,您应该拥有可工作且最新的依赖版本:

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

我认为@Josh是正确的。在许多*nix平台上,默认情况下,数据库设置仅允许本地域套接字而不是tcp。检查您的postgresql配置,并确保您可以使用用户名和密码连接到数据库。 - Tim X

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接