Emacs,sql-mode,Postgresql和输入密码

Jam*_*own 7 postgresql emacs sql-mode

我正在尝试使用sql-mode通过Emacs连接到PostgreSQL数据库.我启动Emacs,命令M-x sql-postgres并提示用户,数据库和服务器,但不提示密码.打开一个空缓冲区,无论我写什么,我得到:

the Password for user james:
psql: FATAL:  password authentication failed for user "james"
Run Code Online (Sandbox Code Playgroud)

不过我可以使用psql登录.我在Linux Antergos中运行GNU Emacs 24.4.1,PostgreSQL是9.3.5.

谢谢..

小智 10

如果其他人来看我在sql-postgres命令完成后通过调用send-invisible并输入我的密码来解决这个问题.Emacs版本24.5.1.

  1. 启动postgres连接
  2. 在空白屏幕Mx发送 - 隐形
  3. 输入密码.
  4. 利润.


phi*_*ils 6

Kevin的回答提供了一个交互式解决方案.

否则,您必须使用非交互式身份验证方法(例如.pgpass文件).


我的原始(和不正确的)建议是启用"密码"选项M-x customize-option RET sql-postgres-login-params RET.

sql-*-login-params变量(和相关定义窗体)会在所有类型的数据库推广; 但并非所有这些选项都适用于所有数据库.

在适用的情况下,密码由Emacs读取,然后在命令行中使用.psql但是,不允许将密码作为命令的一部分提供,因此Emacs无法在此处使用此方法.这就是为postgres登录参数禁用密码选项的原因.


mem*_*lex 6

Here is a simpler solution exploiting the fact that you can pass a password in the connection string. For example:

(setq sql-connection-alist
      '((my-db (sql-product 'postgres)
               (sql-database "postgresql://user:pass@host/database"))))
Run Code Online (Sandbox Code Playgroud)

Then disable all postgres login params except for database.


As a bonus here is my config for using the unix password manager pass which I recommend:

(defun my-pass (key)
  (string-trim-right
   (shell-command-to-string (concat "pass " key))))

(setq sql-connection-alist
      '((db (sql-product 'postgres)
            (sql-database (concat "postgresql://user:"
                                  (my-pass "db/user")
                                  "@host/database")))))
Run Code Online (Sandbox Code Playgroud)


Lor*_*sum 6

我发现在sql-connection-alist. 定义连接后,您可以使用 进行连接sql-connect。可以使用上/下/上一个/下一个来选择特定连接。避免通过在连接期间读取密码来存储密码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")))))
Run Code Online (Sandbox Code Playgroud)

首先,清除sql-postgres-login-params变量。创建默认值是没有意义的,因为它们将在连接列表中定义。如果sql-postgres-login-params非零,连接时会提示您。这很烦人,并且违背了显式定义连接的目的。

其次,使用“连接 URI”来定义连接。

它们的形式如下:

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我们正在创建名为primary-dbsecondary-dbfor的连接postgres。连接仅使用“数据库”参数。数据库是通过将字符串连接成适当形式而形成的连接 URI。看看如何read-passwd阻止我们存储密码?将每个参数替换为您自己的参数。请注意主机 ( @)、端口 ( :) 和数据库 ( /) 的各种语法元素。如果您不想存储特定参数,我想您可以read-string像使用 一样使用read-passwd,但这基本上是重新发明使用 的工具sql-postgres-login-params

请参阅C-h i d g (elisp) Association Lists参考资料 了解有关 alist 的更多信息。