spec and pool read PGHOST, PGPORT, PGUSER, PGDATABASE and PGPASSWORD from the environment and fall back to ~/.pgpass for the password. Function arguments override anything from the environment.
(require '[psql.core :as pg]
'[next.jdbc :as jdbc])
;; A plain db-spec, suitable for any next.jdbc call.
(def db (pg/spec))
(jdbc/execute! db ["SELECT 1 AS one"])
;; A HikariCP-pooled datasource.
(def pooled (pg/pool :host "db1.example.com"
:user "myaccount"
:dbname "anotherdb"
:password "foobar"
:hikari {:read-only true}))
(jdbc/execute! pooled ["SELECT 'hello from db'"])
(pg/close! pooled)
Delay creation so connection parameters are not resolved (and the pool is not opened) at load time:
(def db (delay (pg/pool)))
(jdbc/execute! @db ["SELECT 1"])
spec resolves its map as follows:
:dbtype defaults to "postgresql"; the current OS username seeds :user and :dbname (as psql does).PGHOST / PGPORT / PGUSER / PGDATABASE override :host / :port / :user / :dbname.spec arguments override everything above.:password, then PGPASSWORD, then a ~/.pgpass match.Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |