Search code examples
rubypostgresqldatabase-connectionident

Ruby -> PostgreSQL connection with pg_hba.conf set to "ident sameuser" instead of "trust"


I've tried every Google search term I can think of but everything I dig up keeps saying to set local connections to trust in pg_hba.conf (seems like a security hole if anyone locally can log in and access the DB as anyone they say they are).

In pg_hba.conf local connections are set to ident sameuser. The script should be running as that user, but I get this error:

A database error occurred: 
fe_sendauth: no password supplied

The Ruby code is pretty generic:

conn_str = "DBI:pg:dbname=mydb;host=" + localhost
@connection = DBI.connect(conn_str, "myuser", '')

I can work around this by creating a ~/.pgpass file as described here, but I'd prefer being able to let users log in and just access the DB server.

Anyone ever been able to get PostgreSQL's ident sameuser to work properly for local scripts?


Solution

  • I suspect this:

    In pg_hba.conf local connections are set to ident sameuser. The script should be running as that user, but I get this error [...]

    conn_str = "DBI:pg:dbname=mydb;host=" + localhost
    @connection = DBI.connect(conn_str, "myuser", '')
    

    Please note that a "local" connection is not the same as a connection to "localhost". As soon as you mention "localhost" in the connection URL a TCP/IP socket is created. These are managed by the host rules in `pg_hba.conf.

    To use a real "local" connection Unix Domain Sockets must be used. But I don't know whether or not the Ruby DBI connector supports them.