我发现在sql-connection-alist
. 定义连接后,您可以使用sql-connect
. 可以使用 up/down/previous/next 选择特定的连接。避免通过在连接期间读取密码来存储密码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")))))
首先,清除sql-postgres-login-params
变量。创建默认值没有意义,因为它们将在连接列表中定义。如果sql-postgres-login-params
为非零,连接时会提示您。这很烦人,并且违背了明确定义连接的目的。
其次,使用“Connection URIs”来定义一个连接。
它们具有以下形式:
postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
在上面的示例中,我们正在创建名为primary-db
和secondary-db
for 的连接postgres
。连接仅使用“数据库”参数。数据库是通过将字符串连接成适当的形式而形成的连接 URI。看看如何read-passwd
阻止我们存储密码?用您自己的参数替换每个参数。请注意主机 ( @
)、端口 ( :
) 和数据库 ( /
) 的各种语法元素。如果您不想存储特定参数,我想您可以read-string
像 with 一样使用read-passwd
,但这基本上是在重新发明使用sql-postgres-login-params
.
有关列表C-h i d g (elisp) Association Lists
的更多信息,请参阅。