0

This other Question has Answers about viewing and editing settings in postgresql.conf file using pgAdmin 4. But those Answers did not mention editing the pg_hba.conf file used for authentication of users.

➥ How to use pgAdmin 4 for editing the pg_hba.conf file?

1

No, pgAdmin cannot edit pg_hba.conf

Unfortunately, there is no mechanism by which pgAdmin can edit a pg_hba.conf file. The reason why it's possible to edit postgresql.conf via pgAdmin is that the ALTER SYSTEM syntax is something that can be entered into a client interface. Therefore, pgAdmin was able to "edit" postgresql.conf because it could send an ALTER SYSTEM query to the database. No such syntax exists for editing access control.

  • So what is the usual mechanism for editing pg_hba.conf? Do folks just run a text editor like nano using permissions of the postgres user? – Basil Bourque Nov 10 at 7:09
  • Yes, the typical method of editing pg_hba.conf is with the text editor of your choice. – richyen Nov 10 at 7:19
1

The correct Answer by richyen explains how the host-based access settings of pg_hba.conf file are not editable by pgAdmin 4. So we must use a text-editor to edit the pg_hba.conf file directly.

Example on macOS

Here is an example of using an editor to do so, the command-line tool nano, on macOS (and possibly on BSD).

The Postgres database system is installed along with a new system-level user, by default named postgres. That user owns the folders (has file system access privileges) containing the actual Postgres database files. So we must run as that user.

Switching users is surprisingly complicated if you are new to Unix-oriented OSes. For details, see: Switch user to 'postgres' user on macOS results in “su: Sorry” error.

To switch users within a terminal session using su, we must do so by invoking the superuser's authority using sudo.

sudo su postgres

You will then be prompted for the password of the superuser.

Verify which user is currently running the session by doing:

whoami

Before editing, we should back-up the pg_hba.conf file. Make a copy, giving the file a similar name, with something like .backup appended.

  1. Switch to the data folder nested within the postgres user’s home folder. Type:
    cd ~/data
  2. To see a listing of files and verify pg_hba.conf, run: ls
  3. Run:
    copy pg_hba.conf pg_hba.conf.backup
  4. Verify the backup by running ls again.

Now we can edit the original using the nano editor.

nano pg_hba.conf

You will see the file’s content appear on-screen. Notice the keystroke commands listed at bottom with ^ (Control key).

Use the down-arrow key to get to the end of the file (or press keys for Next Page). Use right-arrow key to move through the lines to edit. When done, press Control-O (for write Out) to save the file. See the reminder the Control+x will exit the nano editor.

At the command line, to stop running that session as the postgres user, run exit. Then verify again with whoami.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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