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:
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