R
Railway•2y ago
onurgu

Postgresql "Operation timed out"

sqlalchemy.exc.DatabaseError: (psycopg2.DatabaseError) could not receive data from server: Operation timed out
sqlalchemy.exc.DatabaseError: (psycopg2.DatabaseError) could not receive data from server: Operation timed out
I get this message on the backend logs very frequently while developing locally. But sometimes it works. What is the reason?
30 Replies
Percy
Percy•2y ago
Project ID: 18b66c44-d5b0-474f-bfea-26659858c9ff
onurgu
onurguOP•2y ago
18b66c44-d5b0-474f-bfea-26659858c9ff
Brody
Brody•2y ago
when developing locally are you starting your app with either railway run or railway shell ?
onurgu
onurguOP•2y ago
I'm using the usual command for FastAPI uvicorn app:app --reload
Brody
Brody•2y ago
yes but are you using it with either of those two commands i mentioned?
onurgu
onurguOP•2y ago
no
Brody
Brody•2y ago
how would you get access to your database variables then?
onurgu
onurguOP•2y ago
I use the PostgreSQL address directly I guess? SQLALCHEMY_DATABASE_URL = "postgresql://postgres:[email protected]:7865/railway"
Brody
Brody•2y ago
you never wanna hardcode credentials
onurgu
onurguOP•2y ago
hmm where is the best practice for this?
Brody
Brody•2y ago
show me a screenshot of your service variables so i can advice further
onurgu
onurguOP•2y ago
I saw this now on one of your pages: DATABASE_URL=${{Postgres.DATABASE_URL}} where should I write this?
Brody
Brody•2y ago
^ service variables, not your code
onurgu
onurguOP•2y ago
do you mean this page?
Brody
Brody•2y ago
the service variables
onurgu
onurguOP•2y ago
I don't understand?
onurgu
onurguOP•2y ago
Brody
Brody•2y ago
yes! okay now do what that info banner wants you to https://docs.railway.app/develop/variables#reference-variables
onurgu
onurguOP•2y ago
OK I added DATABASE_URL
Brody
Brody•2y ago
then show me another screenshot once you have a DATABASE_URL reference variable
onurgu
onurguOP•2y ago
Brody
Brody•2y ago
perfect now instead of using a hardcoded string, use os.getenv("DATABASE_URL") in place of the hardcoded string and do you have the railway cli installed?
onurgu
onurguOP•2y ago
OK I did it and run railway run uvicorn blabla it worked but will it solve my connection timeout problem?
Brody
Brody•2y ago
in a perfect world, yes
onurgu
onurguOP•2y ago
🙂 OK I'll be asking here again if I get the problem again
Brody
Brody•2y ago
so you have ran that command and your code has connected to the database?
onurgu
onurguOP•2y ago
yes
Brody
Brody•2y ago
okay great, that should be all thats needed now if your code has any other hard coded secrets or credentials add them to the railway service variables, then access them the same way you access the DATABASE_URL variable
onurgu
onurguOP•2y ago
I don't think there is any other. Thank you I'll do that this way from now on.
Brody
Brody•2y ago
awsome, happy to help, come back if you have any other problems!
Want results from more Discord servers?
Add your server