Failed connection to the server PGSQL.5432 when running sidekiq job

Hi,

I’m have web, sidekiq, redis and a db setup. I’ve been running into an error when connecting to PGSQL when a background job runs.

Sep 15 11:36:00 AM  2023-09-15T10:36:00.740Z pid=71 tid=l1n WARN: {"context":"Job raised exception","job":{"retry":true,"queue":"default","args":[14],"class":"ActionJob","jid":"41350831e723b0cafaaa67ea","created_at":1694772978.138218,"enqueued_at":1694774160.737184,"error_message":"connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: No such file or directory\n\tIs the server running locally and accepting connections on that socket?\n","error_class":"ActiveRecord::ConnectionNotEstablished","failed_at":1694772978.147657,"retry_count":5,"retried_at":1694773475.4626},"_config":{"labels":"#<Set: {}>","require":".","environment":"production","concurrency":5,"timeout":25,"poll_interval_average":null,"average_scheduled_poll_interval":5,"on_complex_arguments":"raise","error_handlers":["#<Proc:0x000055ec7d363f98 /opt/render/project/.gems/ruby/2.7.0/gems/sidekiq-7.1.4/lib/sidekiq/config.rb:37 (lambda)>"],"death_handlers":[],"lifecycle_events":{"startup":[],"quiet":[],"shutdown":[],"heartbeat":[],"beat":["#<Proc:0x000055ec7d2412c8 /opt/render/project/.gems/ruby/2.7.0/gems/sidekiq-7.1.4/lib/sidekiq/metrics/tracking.rb:133>"]},"dead_max_jobs":10000,"dead_timeout_in_seconds":15552000,"reloader":"#<Sidekiq::Rails::Reloader @app=Formless::Application>","backtrace_cleaner":"#<Proc:0x000055ec7e143ca8 /opt/render/project/.gems/ruby/2.7.0/gems/sidekiq-7.1.4/lib/sidekiq/rails.rb:59 (lambda)>","queues":["default"],"tag":"src","identity":"srv-ck226ho21fec73alnbvg-b45db9f4f-t8kph:71:4fc021d27b91"}}
Sep 15 11:36:00 AM  2023-09-15T10:36:00.740Z pid=71 tid=l1n WARN: ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Sep 15 11:36:00 AM      Is the server running locally and accepting connections on that socket?
Sep 15 11:36:00 AM  
Sep 15 11:36:00 AM  2023-09-15T10:36:00.741Z pid=71 tid=l1n WARN: app/services/action_service/action_creator.rb:18:in `call'
Sep 15 11:36:00 AM  app/sidekiq/action_job.rb:5:in `perform'

Render.yaml looks like this:

databases:
  - name: formless
    databaseName: formless_db
    user: formless_db_user

services:
  - type: redis
    name: sidekiq-redis
    region: oregon
    maxmemoryPolicy: noeviction
    ipAllowList: [] # only allow internal connections

  - type: worker
    name: sidekiq-worker
    runtime: ruby
    region: oregon
    buildCommand: bundle install
    startCommand: bundle exec sidekiq
    envVars:
      - key: REDIS_URL
        fromService:
          type: redis
          name: sidekiq-redis
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false
  - type: web
    name: formless
    runtime: ruby
    region: oregon
    buildCommand: bundle install; bundle exec rake assets:precompile; bundle exec rake assets:clean;
    startCommand: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
    envVars:
      - key: REDIS_URL
        fromService:
          type: redis
          name: sidekiq-redis
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false

And I have the ENV keys setup in the dashboard.

My database.yml looks like this:

# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: formless_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  #username: formless_user

  # The password associated with the postgres role (username).
  #password: <%= ENV['FORMLESS_DATABASE_PASSWORD'] %>

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: formless_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Anything I could try to resolve this?

Thanks in advance!

Does the worker need a DATABASE_URL value as well perhaps? Much like you’re doing for Sidekiq, you’d need the same there to set it to the value of the provisioned PG.

John B
Render Support, UTC+1 :uk:

Hi John, thanks for getting back so fast. I was following this doc Deploy Rails with Sidekiq on Render | Render.

I’ve now added DATABASE_URL to the Sidekiq ENV on the dashboard. It begins… postgres://formless_db_user:.... It is still showing the same error.

Do you think it may be worth starting over? The web app is connecting to the DB ok and I’m able to persist data.

Could the error you are seeing be from when it previously occurred, and the service has been restarted, and you are now seeing new log lines? Worth checking the timestamp of when the error occurred?

John B
Render Support, UTC+1 :uk:

Hi John, I think what you suggested solved the issue. Thanks for the help.

1 Like