I have the following configuration in emacs init files to set a variable (user) to the current Unix login user:

(setq sql-postgres-login-params
      `((user :default ,user-login-name)
        (database :default ,user-login-name)
    (server :default "")
        (port :default 5432)))

With this, I was able to login into PostgreSQL databases with the Unix login user as the default database user. However, this stops working when the Unix user name contains dashes -, and needs to be double-quoted. For example, in PostgreSQL, when the unix user is myuser, it's OK to use it directly as the database user name. However, when the user name is my-user, it needs to be quoted as "my-user" to be used as a user name (or database name). My question is:

How can I make the user-login-name in the above example quoted?

e.g. if ,user-login-name is my-user, how do I use "my-user" instead?

I am not familiar with elisp, and the below is the best I can come up with (concatenating with "\""s). But database login still fails.

(setq sql-postgres-login-params
      `((user :default ,(concat "\"" user-login-name "\"") )
        (database :default ,(concat "\"" user-login-name "\"") )
        (server :default "")
        (port :default 5432)))
  • So if using psql on the command line, for instance, you would say psql --username='"my-user"' ?
    – phils
    Commented Aug 27, 2019 at 6:02
  • Are you getting an error message with the failure? Maybe in the *Messages* buffer? If so, please copy it to the question.
    – phils
    Commented Aug 27, 2019 at 6:04
  • 1
    In any case, if you could provide an example of a working psql command, that would be very useful.
    – phils
    Commented Aug 27, 2019 at 6:20
  • I think the scope of this question is very specifically about sql-postgres, so I've made some edits. FYI as far as elisp and strings go, there's nothing wrong with your concat usage (or you could alternatively use format). I suspect none of that is actually relevant to your problem, though (and indeed that it's potentially causing additional problems to add those quotes in that context).
    – phils
    Commented Aug 27, 2019 at 7:00
  • @phils, Thanks! Your comments and answer are exactly right. psql works fine on the command line. And there was no errors in Message. Adding the code in your answer fixed the problem.
    – tinlyx
    Commented Aug 27, 2019 at 16:47

1 Answer 1


This isn't what you've asked, but is possibly the actual problem. Does it help?

(add-hook 'sql-interactive-mode-hook 'my-sql-interactive-mode-hook)

(defun my-sql-interactive-mode-hook ()
  "Custom interactive SQL mode behaviours.  See `sql-interactive-mode-hook'."
  ;; Product-specific behaviours.
  (when (eq sql-product 'postgres)
    ;; Allow symbol chars and hyphens in database names in prompt.
    ;; Default postgres pattern was: "^\\w*=[#>] " (see `sql-product-alist').
    (setq sql-prompt-regexp "^\\(?:\\sw\\|\\s_\\|-\\)*=[#>] ")
    ;; Ditto for continuation prompt: "^\\w*[-(][#>] "
    (setq sql-prompt-cont-regexp "^\\(?:\\sw\\|\\s_\\|-\\)*[-(][#>] ")))

n.b. This was the only issue I could see when I did a test with a hyphenated username, and it doesn't prevent login as such, but it does cause problems (which include the login apparently taking a long time, and the SQLi buffer not showing a prompt, which might very well seem like a login failure).

  • n.b. I believe you'll want to revert your sql-postgres-login-params changes before testing.
    – phils
    Commented Aug 27, 2019 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.