I find it simplest to explicitly define the connections within the sql-connection-alist
. Once the connections are defined, you can connect using sql-connect
. A particular connection can be selected using up/down/previous/next. Avoid storing passwords by reading them in during connection with read-passwd
.
;; Remove all default login parameters
(setq sql-postgres-login-params nil)
;; define your connections
(setq sql-connection-alist
'((primary-db (sql-product 'postgres)
(sql-database (concat "postgresql://"
"username" ;; replace with your username
":" (read-passwd "Enter password: ")
"@host" ;; replace with your host
":port" ;; replace with your port
"/database" ;; replace with your database
)))
(secondary-db (sql-product 'postgres)
(sql-database (concat "postgresql://"
"username:"
(read-passwd "Enter password: ")
"@host"
":port"
"/database")))))
First, clear out the sql-postgres-login-params
variable. There's no sense creating defaults since they'll be defined in the connections list. If sql-postgres-login-params
is non-nil, you'll be prompted when connecting. This is annoying and defeats the purpose of explicitly defining connections.
Second, use "Connection URIs" to define a connection.
These have the form:
postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
In the example above, we are creating connections called primary-db
and secondary-db
for postgres
. The connections use only the "database" parameter. The database is a connection URI made by concatenating strings into the appropriate form. See how read-passwd
prevents us from storing the password? Replace each parameter with your own. Pay attention to the various syntax elements for host (@
), port (:
), and database (/
). If you don't want to store a particular parameter, I suppose you could use read-string
just as with read-passwd
, but that's basically reinventing the tooling which uses sql-postgres-login-params
.
See C-h i d g (elisp) Association Lists
for more about alists.