10

What do I put in ~/.psqlrc file to set the default editor?

The editor I want to use (Textmate) is at /Users/username/bin/mate.

I found the place in the docs where they talk about it, and tried several syntax variations, but can't seem to get it to work.

For example, this didn't work...

\set PSQL_EDITOR "/Users/username/bin/mate"

In psql when I enter \e I get...

sh: /usr/local/bin/mate: No such file or directory
sh: line 0: exec: /usr/local/bin/mate: cannot execute: No such file or directory
4
  • 3
    I suspect /Users/username/bin/mate is a symbolic link to /usr/local/bin/mate, which doesn't exist.
    – Philᵀᴹ
    Commented Jun 29, 2012 at 7:18
  • Yes, it's a symlink. I tried setting it to the direct path to the executable. Same result.
    – Ethan
    Commented Jun 29, 2012 at 17:20
  • Are you unable to change the default editor at all or is it just Textmate you can't change it to? Commented Jul 1, 2012 at 18:01
  • /Users/ OK so it's a Mac I don't recall how to set environment variables on a Mac, but that's what you need to do.
    – Jasen
    Commented Dec 14 at 12:31

6 Answers 6

15

Contrary to my comment above (which was the result of insufficient testing), you should put a line into .bashrc (or any similar place):

export PSQL_EDITOR="/Users/username/bin/mate"

In psql coming with PostgreSQL 9.2, there is a \setenv command, it is supposed to work from .psqlrc as well.

(Credits to Pedro Romano.)

2
  • This worked for me on Debian 7 with Postgres 9.3.
    – RichVel
    Commented Jul 30, 2015 at 17:07
  • btw \setenv does not expect a =, it need to be written like that: \setenv PSQL_EDITOR "/Users/username/bin/mate" Commented Dec 18, 2019 at 18:58
3

The first time you use editor psql asks you to choose one of them.

# \e
usage: which [-as] program ...
usage: which [-as] program ...
usage: which [-as] program ...

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/code
  4. /usr/bin/vim.tiny

Choose 1-4 [2]: 

The chosen editor is recorded at $HOME/.selected_editor. If you choose the easiest one then this file is empty but if you choose another option then this is its contents:

# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/vim.tiny"

Edit this file and you don't need to set any environment variable.

1

Try setting it to whatever you type to run the application from the shell. If that doesn't work, try running the editor from the shell to troubleshoot.

0

I use Notepad++ as a text editor. Running Windows 10.

You need an etc folder in the Postgres root folder (this folder already has bin, lib, data if it's a server). Create a text file psqlrc (no file extension).

Contents of my psqlrc are:

\! chcp 65001
\setenv PSQL_EDITOR 'C:\\Program Files\\Notepad++\\notepad++.exe'
SET CLIENT_ENCODING = 'UTF8';
0

I am an windows user and want to set default editor as [Subtext]. Actually, I search a lot but none of them work for me. I finally discover that EDITOR or PSQL_EDITOR are environmen variables of psql, so I use command

\setenv PSQL_EDITOR filename

It works for me.

0

Try to run utility select-editor (/usr/bin/select-editor)

Psql (and not only psql) first takes the editor from the file ~\.selected_editor, then from the environment variables. If this file is empty - in my case psql takes nano.

New contributor
xGluck is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.