0

I'm trying to connect to Postgres in a C program using libpq. When I'm running under my own username (which is the same as the username I've created for my Postgres database) everything is fine. When I try to run the program under root it fails with:

FATAL:  Peer authentication failed for user "jqpublic"

In both cases the conninfo string I'm passing to PQconnectdb is something like:

dbname=somedb user=jqpublic password=somepassword

If I add a host option to the conninfo, the connection succeeds, but then I'll be connecting over TCP/IP which my tests show is measurably slower than the default UNIX sockets. Is there a way to use the UNIX socket while running under root (or another user with a name not the same as the DB user)?

Elektito
  • 3,863
  • 8
  • 42
  • 72
  • Read the manual on `pg_hba.conf`, specifically the `local` entries – Craig Ringer Nov 23 '15 at 10:47
  • @CraigRinger It says to use "peer" authentication for local connections which apparently doesn't work for the root user. – Elektito Nov 23 '15 at 12:21
  • It works fine, but only if you're connecting as a database user with the name `root` as well. You can use an `ident.conf` mapping to allow the unix user `root` to connect as other users. Or you can change the auth method to `md5` (password). Or, if security isn't important for users with access to the local system, `trust`. See the manual for details. – Craig Ringer Nov 23 '15 at 16:04
  • @CraigRinger Ah! I finally got what is going on. I thought there might be a programmatic answer to this but apparently not. Changing "local" authentication to "md5" did the trick (after I remembered I have to have Postgres reload its configuration that is). Thanks a lot. Care to post that as an asnwer? – Elektito Nov 23 '15 at 17:35

0 Answers0