How do i link my postgresql to my python server so i dont have to link it in the code?
pygaps-dev
TRIALOP

2 years ago

Title

Solved

131 Replies

pygaps-dev
TRIALOP

2 years ago

8f22bd42-f5e7-4c51-b7de-0fb5ac0c98b7


2 years ago

Environment variables




pygaps-dev
TRIALOP

2 years ago

os.getenv('DB_NAME') ?


pygaps-dev
TRIALOP

2 years ago

thats just an example btw


2 years ago


value = os.getenv("VAR_NAME")  # Replace VAR_NAME with the actual variable name
print(value)

pygaps-dev
TRIALOP

2 years ago

Okay so


2 years ago

don't actually print your secrets


pygaps-dev
TRIALOP

2 years ago

DB_PARAMS = {
    'dbname': os.getenv('PGDATABASE'),
    'user': os.getenv('PGUSER'),
    'password': os.getenv('PGPASSWORD'),
    'host': os.getenv('PGHOST'),
    'port': os.getenv('PGPORT')
}

pygaps-dev
TRIALOP

2 years ago

?


2 years ago

I'd assume so - port may throw type errors (unsure how python likes this)


2 years ago

my python knowledge is sketchy at best


pygaps-dev
TRIALOP

2 years ago

Oh


pygaps-dev
TRIALOP

2 years ago

Lemme try it rq


pygaps-dev
TRIALOP

2 years ago

Hmph


pygaps-dev
TRIALOP

2 years ago

I tried it


pygaps-dev
TRIALOP

2 years ago

@BillyDotWS


pygaps-dev
TRIALOP

2 years ago

I'm getting an error


pygaps-dev
TRIALOP

2 years ago

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?

2 years ago

are you positive it's running on 5000?


2 years ago

normally runs on 5432 by default


2 years ago

  • are you running this on railway or locally?

2 years ago

this won't work locally, you'll need to use the public domain


pygaps-dev
TRIALOP

2 years ago

yes


pygaps-dev
TRIALOP

2 years ago

the postgresql im running it on railway


2 years ago

and your service?


2 years ago

or are you running the service locally


pygaps-dev
TRIALOP

2 years ago

python server too


pygaps-dev
TRIALOP

2 years ago

on railway


pygaps-dev
TRIALOP

2 years ago

the same project


pygaps-dev
TRIALOP

2 years ago

two diff services


pygaps-dev
TRIALOP

2 years ago

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


pygaps-dev
TRIALOP

2 years ago

thats what i


pygaps-dev
TRIALOP

2 years ago

usualyl do


pygaps-dev
TRIALOP

2 years ago

in my python apis


2 years ago

can you change port back to 5432 on both


pygaps-dev
TRIALOP

2 years ago

sure


2 years ago

I think you have to do a tad extra to change the port postgres runs on on the postgres side


2 years 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


pygaps-dev
TRIALOP

2 years ago

alrightyy


pygaps-dev
TRIALOP

2 years ago

im redeploying the python api


pygaps-dev
TRIALOP

2 years ago

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"

2 years ago

progress!


pygaps-dev
TRIALOP

2 years ago

I see that the password authentication failed


2 years ago

so, is the password correct


pygaps-dev
TRIALOP

2 years ago

I linked the password


2 years ago

and you didn't change that, right?


pygaps-dev
TRIALOP

2 years ago

1336411510326431744


pygaps-dev
TRIALOP

2 years ago

dont know why


pygaps-dev
TRIALOP

2 years ago

nope


pygaps-dev
TRIALOP

2 years ago

i didnt change it


2 years ago

check PGPASSWORD is filled with a print


2 years ago

I've seen a couple cases on my dashboard where it goes missing randomly


pygaps-dev
TRIALOP

2 years ago

what do you mean filled with a print?


2 years ago

print(os.getenv('PGPASSWORD'))


2 years ago

or a null check


pygaps-dev
TRIALOP

2 years ago

it has random letters and numbers


pygaps-dev
TRIALOP

2 years ago

and does not have a print


2 years ago

check it's in your container runtime


pygaps-dev
TRIALOP

2 years ago

oh


2 years ago

(null check is probably a better idea if this is a prod app)


pygaps-dev
TRIALOP

2 years ago

wait


pygaps-dev
TRIALOP

2 years ago

connection to server at "postgres.railway.internal" (fd12:464e:84c::6a:9860:cc56), port 5432 failed: FATAL: password authentication failed for user "postgres"


pygaps-dev
TRIALOP

2 years ago

this means both the port and authentication failed?


2 years ago

nope, just auth


pygaps-dev
TRIALOP

2 years ago

port 5432 failed: FATAL


2 years ago

if you see auth failed, the connection is fine


2 years ago

that's just the address details


pygaps-dev
TRIALOP

2 years ago

oh


pygaps-dev
TRIALOP

2 years ago

okay so i dont get what you mean the pgpassword is filled with a print😭


2 years ago

In your code - check the environment variable has stuff in it


pygaps-dev
TRIALOP

2 years ago

it links the connection using this:

def get_db_connection():
    conn = psycopg2.connect(**DB_PARAMS)
    return conn

and 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')
}

pygaps-dev
TRIALOP

2 years ago

environnement variable has no stuff in it, it just uses os to get the environnement variable


pygaps-dev
TRIALOP

2 years ago

?


pygaps-dev
TRIALOP

2 years ago

@BillyDotWS


pygaps-dev
TRIALOP

2 years ago

any idea?


2 years ago

Check that your environment variables are all setup correctly

print(os.getenv('PGDATABASE'))
print(os.getenv('PGUSER'))
print(os.getenv('PGPASSWORD'))
print(os.getenv('PGHOST'))
print(os.getenv('PGPORT'))

pygaps-dev
TRIALOP

2 years ago

Alrighty


2 years ago

your postrgres password got changed, i've reverted that for you


pygaps-dev
TRIALOP

2 years ago

oh alright


2 years ago

looks like you are hardcoding the password in your code now, please use only environment variables


pygaps-dev
TRIALOP

2 years ago

oh


pygaps-dev
TRIALOP

2 years ago

yeah


pygaps-dev
TRIALOP

2 years ago

wait ill change it


pygaps-dev
TRIALOP

2 years ago

that was earlier before u told me the postgres pass was changed


pygaps-dev
TRIALOP

2 years ago

but now ive fixed it


pygaps-dev
TRIALOP

2 years ago

im redeploying


pygaps-dev
TRIALOP

2 years ago

great it worked!


pygaps-dev
TRIALOP

2 years ago

thanks!


2 years 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.

2 years ago

you need to be using gunicorn


pygaps-dev
TRIALOP

2 years ago

do i need to replace my flask application with gunicorn?


pygaps-dev
TRIALOP

2 years ago

or just like setup gunicorn?


pygaps-dev
TRIALOP

2 years ago

ive never used gunicorn


2 years ago

you need to run your flask app with gunicorn


pygaps-dev
TRIALOP

2 years ago

alrighty


pygaps-dev
TRIALOP

2 years ago

thanks for the help


pygaps-dev
TRIALOP

2 years ago

u can close this nwo


pygaps-dev
TRIALOP

2 years ago

now*


pygaps-dev
TRIALOP

2 years ago

!close


pygaps-dev
TRIALOP

2 years ago

uh


pygaps-dev
TRIALOP

2 years ago

😭


pygaps-dev
TRIALOP

2 years ago

is!close


pygaps-dev
TRIALOP

2 years ago

what😭


2 years ago

are you using gunicorn now?


pygaps-dev
TRIALOP

2 years ago

actually


pygaps-dev
TRIALOP

2 years ago

yes


pygaps-dev
TRIALOP

2 years ago

but the app failed to load


pygaps-dev
TRIALOP

2 years ago

[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.


pygaps-dev
TRIALOP

2 years ago

😭


pygaps-dev
TRIALOP

2 years ago

god help me


2 years ago

what is your start command


pygaps-dev
TRIALOP

2 years ago

web: gunicorn -w 4 -b 0.0.0.0:3540 wsgi:app


pygaps-dev
TRIALOP

2 years ago

i js did a fix


pygaps-dev
TRIALOP

2 years ago

lemme see if it works


pygaps-dev
TRIALOP

2 years ago

works!


pygaps-dev
TRIALOP

2 years ago

[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: 7

pygaps-dev
TRIALOP

2 years ago

dont know what this means but it means it works i guess


2 years ago

what was the fix


pygaps-dev
TRIALOP

2 years ago

uhh the wsgi.py was empty💀


pygaps-dev
TRIALOP

2 years ago

😭


pygaps-dev
TRIALOP

2 years ago

i added soem code to it


pygaps-dev
TRIALOP

2 years ago

that makes the db tables


pygaps-dev
TRIALOP

2 years ago

and


pygaps-dev
TRIALOP

2 years ago

runs the app


pygaps-dev
TRIALOP

2 years ago

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


2 years ago

as long as you are listening on the PORT environment variable


pygaps-dev
TRIALOP

2 years ago

yeah


pygaps-dev
TRIALOP

2 years ago

you can close this now


2 years ago

!s


Status changed to Solved brody • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...