Rails 8 app, cannot connect to Postgresql DB : connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed
suzumejakku
HOBBYOP

7 months ago

Hi all,

I have looked at all the threads mentioning this particular problem, but none solved it for me.

I have a Rails 8 app, together with Vue 3 for the front-end, and I am using Vite 7.

I created 2 services in Railway, one for Rails + Vue, another one for my Postgresql database.

In the settings of my Rails service, I have the following variables:

DATABASE_URL = ${{Postgres.DATABASE_URL}}

RAILS_ENV = production

In my database service, I have all the default variables set by Railway, including the "DATABASE_URL".

In my database.yml file, I have:

production:
  primary: &primary_production
    <<: *default
    urL: <%= ENV["DATABASE_URL"] %>

And when I try to fire up my Rails service, I get the following message:

Jul 12 23:13:15

Tasks: TOP => db:prepare

Jul 12 23:13:15

(See full trace by running task with --trace)

Jul 12 23:13:19

bin/rails aborted!

Jul 12 23:13:19

ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (ActiveRecord::ConnectionNotEstablished)

Jul 12 23:13:19

Is the server running locally and accepting connections on that socket?

Jul 12 23:13:19

Jul 12 23:13:19

Jul 12 23:13:19

Caused by:

Jul 12 23:13:19

PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (PG::ConnectionBad)

Jul 12 23:13:19

Is the server running locally and accepting connections on that socket?

The messages comes up a few times and then the service crashes. I don't know what I can do to solve this, I can't see any issue with my setup. I just hope one of you geniuses can help me...

Thanks !

Solved$10 Bounty

Pinned Solution

suzumejakku
HOBBYOP

7 months ago

Thanks for the comment. I have to say to this is not really reassuring grinning emoji I just added a second database for solid_cache, and it worked right away ... as I thought it would 3 days ago. I'll keep this thread opened for a few more days in case I encounter another issue with these ENVs...

11 Replies

Railway
BOT

7 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Railway

Hey there! We've found the following might help you get unblocked faster: - [🧵 Error deploying Rails app - Database URL cannot be empty](https://station.railway.com/questions/error-deploying-rails-app-database-url-58684e45) - [🧵 Deployment crashed due to server connection not established (postgresql)](https://station.railway.com/questions/deployment-crashed-18e2f0db) - [🧵 Issue running rake or seeds file from Rails to connected Postgres](https://station.railway.com/questions/issue-running-rake-or-seeds-file-from-ra-a303097d) If you find the answer from one of these, please let us know by solving the thread!

suzumejakku
HOBBYOP

7 months ago

I already found those answers, and none got me on the right track.


suzumejakku
HOBBYOP

7 months ago

During my many, many failed deployments, I realized one thing: if I set RAILS_ENV to "development" instead of "production", it can connect to the database. No idea why. But I remember seeing an answer from Angelo about this. If I set to "development", I can go a bit further, but then I get an issue with Rails complaining that it cannot find the Vite binary (though when I deploy my server, I can see Vite building the chunks). I'm guessing the Vite error happens because it believes it is in a dev environment and expects on-the-fly creation javacsript chunks... whereas if it was in production (i.e. RAILS_ENV = production), it would know that chunks were already created and use them instead. Something like that ?


suzumejakku
HOBBYOP

7 months ago

Continuing on my quest to try understand why RAILS_ENV=development works and not RAILS_ENV=production. Something bothers me : in my deployment.yml, the "development" database does not at all use the DATABASE_URL environment variable. Why then does it work when deploying in Railway ? Or maybe... in fact it doesn't work, it's just Rails (in Railway) ignores it for now, as another issue is happening with Vite (as stated above), and Rails didn't yet have the chance to try and connect to the db. Back to square one: why can't the Rails service connect to the DATABASE_URL when RAILS_ENV=production ?

(sorry, just talking to myself, trying to understand what's going on...)


suzumejakku
HOBBYOP

7 months ago

... and now I could get it running with RAILS_ENV=production but... I had to copy-paste the entire URL of the database in the database.yml (with the username and pwd). So there is something wrong when using the variables in Railway (i.e. if I refer to DATABASE_URL in database.yml, my app cannot connect to the db).


suzumejakku
HOBBYOP

7 months ago

No idea why, but now it works. I went from setting the public URL of the database directly in deploy.yml; it worked. Then I tried with the internal URL of the database; it worked. Finally I updated the deploy.yml using <%=ENV['DATABASE_URL] %> (so going back to the state I was at the beginning), updated the DATABASE_URL to ${{Postgres.DATABASE_URL}} (in my Rails service)... and it worked. I'm keeping this thread opened though, in case it breaks again.


turborx
FREE

7 months ago

hi, your rails app is trying to connect via a local socket because DATABASE_URL isn’t being picked up in production. make sure it’s set correctly in railway, referenced properly in database.yml, and that you redeploy after setting it. that should fix the connection issue.


turborx

hi, your rails app is trying to connect via a local socket because DATABASE_URL isn’t being picked up in production. make sure it’s set correctly in railway, referenced properly in database.yml, and that you redeploy after setting it. that should fix the connection issue.

suzumejakku
HOBBYOP

7 months ago

Thanks for the feedback and suggestion. As stated in my initial message, I believe my whole setup was correct. database.yml referenced DATABASE_URL, and my Rails app in Railway had a variable called DATABASE_URL = ${{Postgres.DATABASE_URL}} ("Postgres" being the name of the db created in Railway). Still I got the issue about the socket. I tried many different things, and as said in my last post, for some reason, now it works. I really don't understand why it did not work the first time, as in the end I went back to the first setup. It is "solved" I guess, but I don't understand how and why.


turborx
FREE

7 months ago

yeah, this kind of issue often comes down to runtime environment mismatch or delayed env injection. even if DATABASE_URL is correctly set in Railway, the container may not pick it up unless the service is fully redeployed (not just restarted), so the build and runtime layers both re-evaluate env vars

config/database.yml is evaluated at boot, so if ENV["DATABASE_URL"] wasn’t available yet (e.g. build-time vs runtime), Rails might default to the UNIX socket connection

sometimes, cached Docker layers or improper RAILS_ENV values (like accidentally using development in production) can lead to weird fallback behavior

in short: the setup was probably correct, but until the app was fully rebuilt or restarted cleanly, it may have been running stale config or misreading envs.

mystery solved (kinda) but definitely a classic “works after redeploy” bug.


suzumejakku
HOBBYOP

7 months ago

Thanks for the comment. I have to say to this is not really reassuring grinning emoji I just added a second database for solid_cache, and it worked right away ... as I thought it would 3 days ago. I'll keep this thread opened for a few more days in case I encounter another issue with these ENVs...


suzumejakku
HOBBYOP

6 months ago

No more issue for a long time now, I will close this thread.


Status changed to Solved noahd 6 months ago


Loading...