Can't run service with CLI and connect to Postgres service
I have been trying to figure out some basic connections to use "railway run" locally for development, and have struggled where the documentation appears to be out of date or too thin (or where I'm being dumb).
I have a NodeJS express service and a Postgres service. I've (finally) gotten the express service able to connect to Postgres when deployed, but "railway run npm start" locally gives me an ENOTFOUND error accessing meticulous-empthy.railway.internal. (Which is obtained via me defining a DATABASE_URL= ${{ meticulous-empathy.DATABASE_URL }} for the express service)
running railway link -p PROJECTID still asks for which service to connect to (docs don't read like that should happen?) and I'm currently connected to the express service. Is there a step I've missed to make this work locally?
Project ID:
e1777fb1-9108-4e98-8bbd-60602862aa10
9 Replies
Project ID:
e1777fb1-9108-4e98-8bbd-60602862aa10
From skimming other answers, this appears to be working as intended (local deploys will have to connect using public addresses), but I'm unclear how to have my code use variables but have different values for those variables when running using "railway run"
railway link
will ask you what service you want to connect to for running future commands like railway up
But to accomplish what you are trying to do, try running railway shell
after linking to your express service. Then you should be able to run npm start
railway shell
will start a new shell session with your selected services environment variables injected into itRunning
railway shell
tells me I'm in a subshell, and npm start
starts my express server locally, but it dies with the exact same ENOTFOUND message trying to connect to the Postgres service.
And still says the internal address (I'm unclear if that's the expected value)Yes, it will launch a sub shell, but you are not on the internal network, so, you would need to reference your public url for database connection locally
Do I need to add conditional code to check for that? I thought the idea of the variables is that they'd reflect the necessary values.
If the service is not live on railway, you will not be on the internal network, so, you cannot connect to an internal domain
Yes, you said that. My question was how. I don't see how railway shell is useful then. The guides I found for plugins (pre service, I guess?) such as your own blog post suggested I could connect to Postgres without having to write new conditional code.
Railway Blog
Deploy an ExpressJS app with PostgreSQL database
In this post, we will walk you through the process of building and hosting your ExpressJS backend with Railway. From deploying the barebones example to setting up a database (+ adding dummy data to it) to working with it locally, we've got you covered!