Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created August 23, 2012 10:22
Show Gist options
  • Save samaaron/3435248 to your computer and use it in GitHub Desktop.
Save samaaron/3435248 to your computer and use it in GitHub Desktop.
How to suck an entire postgres db into Clojure datastructures in memory
(ns ormclj.pg
(:require [clojure.java.jdbc :as sql]))
(defn pg-db
[hostname port db-name user passwd]
{:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname (str "//" hostname ":" port "/" db-name)
:user user
:password passwd})
(def conn (pg-db "localhost" 5432 "dbname" "user-name" "password"))
(def tables (sql/with-connection conn
(sql/with-query-results rs ["SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"]
(doall (for [row rs] row)))))
(def db
(reduce
(fn [res t]
(assoc res
(keyword (:table_name t))
(sql/with-connection db
(sql/with-query-results rs [(str "select * from " (:table_name t))]
(doall (for [row rs] row))))))
{}
tables))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment