Skip to content

service connection string and environment variables interact differently in ruby-pg than psql #635

@jackc

Description

@jackc

I have a pg_service.conf file with an entry like this:

[my-service]
host=example.com
port=25060
dbname=mydb
user=myuser
sslmode=require

I have an environment variable set:

export PGPORT=5020

psql successfully connects using the service file as it uses the port from the service:

$ psql 'service=my-service'
# succeeded

However, if I try to connect with ruby-pg it fails as the port is taken from the environment instead:

$ ruby -rpg -e 'conn = PG.connect("service=my-service")'
/Users/jack/.local/share/mise/installs/ruby/3.3.7/lib/ruby/gems/3.3.0/gems/pg-1.5.9/lib/pg/connection.rb:709:in `async_connect_or_reset': connection to server at "example.com" (xxxx.xxx.xxx.xxx), port 5020 failed: Operation timed out (PG::ConnectionBad)
	Is the server running on that host and accepting TCP/IP connections?

But it succeeds if the service is passed via PGSERVICE:

$ PGSERVICE=my-service ruby -rpg -e 'conn = PG.connect'
# succeeded

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions