8

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

the Password for user james:
psql: FATAL:  password authentication failed for user "james"

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

谢谢..

4

8 回答 8

11

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

  1. 启动 postgres 连接
  2. 在空白屏幕 Mx 发送不可见
  3. 输入密码。
  4. 利润。
于 2016-09-02T01:20:37.170 回答
6

凯文的回答提供了一个交互式解决方案。

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


我最初的(和不正确的)建议是在M-x customize-option RET sql-postgres-login-params RET.

变量(和相关的sql-*-login-params自定义小部件)在所有数据库类型中通用;但并非所有这些选项都适用于所有数据库。

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

于 2014-11-04T19:18:52.927 回答
6

这是一个更简单的解决方案,它利用了您可以在连接字符串中传递密码这一事实。例如:

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

然后禁用除数据库之外的所有 postgres 登录参数。


作为奖励,这是我使用pass我推荐的 unix 密码管理器的配置:

(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")))))
于 2018-08-14T22:32:03.727 回答
5

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

有关列表C-h i d g (elisp) Association Lists的更多信息,请参阅。

于 2020-12-15T16:52:50.520 回答
4

我通过创建一个存储连接凭据的.pgpass文件暂时解决了这个问题。我对此感到不舒服,并且想要一个需要在登录时输入密码的解决方案。

于 2014-11-04T08:33:34.190 回答
1

我也有这个问题。如果您转到空缓冲区并按回车键,您会在那里得到密码提示吗?我这样做了,当输入密码时,模式工作得很好。

于 2016-03-24T11:21:08.303 回答
1

为我设置PGPASSWORD环境变量工作,例如(setenv "PGPASSWORD" "whatever your password is")

于 2017-07-31T23:48:44.243 回答
0

可以通过猴子补丁sql-comint-postgresPGPASSWORD环境变量与标准连接列表字段一起使用:

(defun sql-comint-postgres (product options &optional buf-name)
  "Create comint buffer and connect to Postgres."
  ;; username and password are ignored.  Mark Stosberg suggests to add
  ;; the database at the end.  Jason Beegan suggests using --pset and
  ;; pager=off instead of \\o|cat.  The later was the solution by
  ;; Gregor Zych.  Jason's suggestion is the default value for
  ;; sql-postgres-options.
  (let ((params
         (append
          (if (not (= 0 sql-port))
              (list "-p" (number-to-string sql-port)))
          (if (not (string= "" sql-user))
              (list "-U" sql-user))
          (if (not (string= "" sql-server))
              (list "-h" sql-server))
          options
          (if (not (string= "" sql-database))
              (list sql-database))))
        (process-environment
         (nconc
          (list (format "PGPASSWORD=%s" sql-password))
          process-environment)))
    (message (prin1-to-string process-environment))
    (sql-comint product params buf-name)))
于 2021-10-01T20:27:41.980 回答