a year ago
Title
131 Replies
a year ago
Environment variables
a year ago
a year ago
On railway:
a year ago
```import os
value = os.getenv("VARNAME") # Replace VARNAME with the actual variable name
print(value)
```
a year ago
don't actually print your secrets
DB_PARAMS = {
'dbname': os.getenv('PGDATABASE'),
'user': os.getenv('PGUSER'),
'password': os.getenv('PGPASSWORD'),
'host': os.getenv('PGHOST'),
'port': os.getenv('PGPORT')
}a year ago
I'd assume so - port may throw type errors (unsure how python likes this)
a year ago
my python knowledge is sketchy at best
Traceback (most recent call last):
File "/app/server.py", line 218, in
create_user_table()
File "/app/server.py", line 32, in create_user_table
conn = get_db_connection()
^^^^^^^^^^^^^^^^^^^
File "/app/server.py", line 27, in get_db_connection
conn = psycopg2.connect(**DB_PARAMS)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "postgres.railway.internal" (fd12:464e:84c::6a:9815:3211), port 5000 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?a year ago
are you positive it's running on 5000?
a year ago
normally runs on 5432 by default
a year ago
are you running this on railway or locally?
a year ago
this won't work locally, you'll need to use the public domain
a year ago
and your service?
a year ago
or are you running the service locally
i dont know how to change the port, what i did was change the "pgport" to 5000 and also change the port in the settings to 5000
a year ago
can you change port back to 5432 on both
a year ago
I think you have to do a tad extra to change the port postgres runs on on the postgres side
a year ago
once we figure out if that's the issue, then you can figure that part out if you want to change it for whatever reason
Traceback (most recent call last):
File "/app/server.py", line 218, in
create_user_table()
File "/app/server.py", line 32, in create_user_table
conn = get_db_connection()
^^^^^^^^^^^^^^^^^^^
File "/app/server.py", line 27, in get_db_connection
conn = psycopg2.connect(**DB_PARAMS)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "postgres.railway.internal" (fd12:464e:84c::6a:9860:cc56), port 5432 failed: FATAL: password authentication failed for user "postgres"
connection to server at "postgres.railway.internal" (fd12:464e:84c::6a:9860:cc56), port 5432 failed: FATAL: password authentication failed for user "postgres"a year ago
progress!
a year ago
so, is the password correct
a year ago
and you didn't change that, right?
a year ago
check PGPASSWORD is filled with a print
a year ago
I've seen a couple cases on my dashboard where it goes missing randomly
a year ago
print(os.getenv('PGPASSWORD'))
a year ago
or a null check
a year ago
check it's in your container runtime
a year ago
(null check is probably a better idea if this is a prod app)
connection to server at "postgres.railway.internal" (fd12:464e:84c::6a:9860:cc56), port 5432 failed: FATAL: password authentication failed for user "postgres"
a year ago
nope, just auth
a year ago
if you see auth failed, the connection is fine
a year ago
that's just the address details
a year ago
In your code - check the environment variable has stuff in it
it links the connection using this:
def get_db_connection():
conn = psycopg2.connect(**DB_PARAMS)
return connand this is the DB_PARAMS:
DB_PARAMS = {
'dbname': os.getenv('PGDATABASE'),
'user': os.getenv('PGUSER'),
'password': os.getenv('PGPASSWORD'),
'host': os.getenv('PGHOST'),
'port': os.getenv('PGPORT')
}environnement variable has no stuff in it, it just uses os to get the environnement variable
a year ago
Check that your environment variables are all setup correctly
import os print(os.getenv('PGDATABASE')) print(os.getenv('PGUSER')) print(os.getenv('PGPASSWORD')) print(os.getenv('PGHOST')) print(os.getenv('PGPORT'))
a year ago
your postrgres password got changed, i've reverted that for you
a year ago
looks like you are hardcoding the password in your code now, please use only environment variables
a year ago
its online, now theres the new problem -
This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.a year ago
you need to be using gunicorn
a year ago
you need to run your flask app with gunicorn
a year ago
are you using gunicorn now?
[2025-02-04 19:43:31 +0000] [1] [ERROR] Worker (pid:4) exited with code 4
[2025-02-04 19:43:31 +0000] [1] [ERROR] Shutting down: Master
[2025-02-04 19:43:31 +0000] [1] [ERROR] Reason: App failed to load.
a year ago
what is your start command
[2025-02-04 19:45:59 +0000] [1] [INFO] Starting gunicorn 23.0.0
[2025-02-04 19:45:59 +0000] [1] [INFO] Listening at: http://0.0.0.0:3540 (1)
[2025-02-04 19:45:59 +0000] [1] [INFO] Using worker: sync
[2025-02-04 19:45:59 +0000] [4] [INFO] Booting worker with pid: 4
[2025-02-04 19:45:59 +0000] [5] [INFO] Booting worker with pid: 5
[2025-02-04 19:45:59 +0000] [6] [INFO] Booting worker with pid: 6
[2025-02-04 19:45:59 +0000] [7] [INFO] Booting worker with pid: 7a year ago
what was the fix
uhh the wsgi.py was empty💀
also when im using my server in a js environnement do i need to include the port? i usually dont do the port nnd it works
a year ago
as long as you are listening on the PORT environment variable
a year ago
!s
Status changed to Solved brody • about 1 year ago
