Tag Archives: pgpass
PostgreSQL’s ~/.pgpass Parser in Common Lisp
Here is a quick and dirty PostgreSQL ~/.pgpass parser, in Common Lisp.
It depends on Split Sequence, conveniently loaded with QuickLisp, like so.
(ql:quickload :split-sequence :silent t)
And here is the actual code. It’s not wrapped in a function, and the other connection parameters are hard coded. This can use some refactoring; particularly with relevance to error handling.
(defvar pguser "user") (defvar pgdb "database") (defvar pghost "hostname") (defvar pgport 5432) (defvar pgpass (handler-case (with-open-file (.pgpass "~/.pgpass" :direction :input :if-does-not-exist nil) (flet ((wildcard-string= (a b) (or (string= a "*") (string= a (princ-to-string b))))) (loop for line = (read-line .pgpass nil) while line when (destructuring-bind (host port db user passwd) (split-sequence:split-sequence #\: line) (if (and (wildcard-string= host pghost) (wildcard-string= port pgport) (wildcard-string= db pgdb) (wildcard-string= user pguser)) passwd)) return it))) (file-error (e) (format *error-output* "Unable to load password file; ~A~%" e) nil))) (format t "Password: ~A~%" pgpass)
Split-Sequence is part of CL-Utilities and Public Domain.
QuickLisp is licensed under an MIT-style license.
The example code above is © 2015 Johann ‘Myrkraverk’ Oskarsson and licensed under the Two-clause BSD license.