R
Railway11mo ago
IssaKass

Add Postgres To Flask

I have some truble connecting Postgresql to Flask and deploying it to Railway App
No description
42 Replies
Percy
Percy11mo ago
Project ID: c6894929-c040-4842-8c83-4516d57a14fe
IssaKass
IssaKassOP11mo ago
c6894929-c040-4842-8c83-4516d57a14fe can anyone help me?
Brody
Brody11mo ago
have you added the database variable references to your backend? https://docs.railway.app/guides/variables#referencing-another-services-variable
IssaKass
IssaKassOP11mo ago
I am new to deploying, its my first app that I will deploy it, it is built with Flask+db+React
Brody
Brody11mo ago
okay that's cool, but that doesn't quite answer the question
IssaKass
IssaKassOP11mo ago
I added this one only to the backend:
IssaKass
IssaKassOP11mo ago
could you also provide what to add in .env for flask?
No description
Brody
Brody11mo ago
you don't need an .env file
IssaKass
IssaKassOP11mo ago
configuration? for example config.py
Brody
Brody11mo ago
this looks good to me, are you using that environment variable in your code
IssaKass
IssaKassOP11mo ago
yes: FLASK_APP=main FLASK_ENV=development FLASK_DEBUG=True
Brody
Brody11mo ago
I'm sorry but that doesn't answer the question
IssaKass
IssaKassOP11mo ago
no Just added it to Railway
Brody
Brody11mo ago
you need to use the DATABASE_URL environment variable in code when connecting to the database
IssaKass
IssaKassOP11mo ago
How?
Brody
Brody11mo ago
that would more so be a question for the postgres client you are using in your code
IssaKass
IssaKassOP11mo ago
haven't understood your phrase ...
Brody
Brody11mo ago
you need to read the documentation for the database client you are using in code
IssaKass
IssaKassOP11mo ago
you told me to remove .env, but it is required to run the app also I have some config.py, """Configuration settings for the Flask application.""" import datetime class Config: """Base configuration class.""" # pylint: disable=too-few-public-methods SECRET_KEY = "de641bd49626e8951d21f788" SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db" SQLALCHEMY_TRACK_MODIFICATIONS = False JWT_SECRET_KEY = "3b6e5e0a75e8581f92110b7f" JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(weeks=4) class DevelopmentConfig(Config): """Development confiugration class.""" # pylint: disable=too-few-public-methods DEBUG = True class TestingConfig(Config): """Testing configuration class.""" # pylint: disable=too-few-public-methods TESTING = True SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db" class ProductionConfig(Config): """Production confiugration class.""" # pylint: disable=too-few-public-methods DEBUG = False SECRET_KEY = "3eaace33b55b8c04ed135319c7c839c7c5935490f4dff183e1dde4dde46353b6" SQLALCHEMY_DATABASE_URI = ( "postgresql://IssaKass:issakass2002@localhost:5432/financial" ) JWT_SECRET_KEY = "403076f90595eca8c0ed389bc51022d11a7f881ccb495958012a96cf6801cb72" CONFIG = { "development": DevelopmentConfig, "testing": TestingConfig, "production": ProductionConfig, "default": DevelopmentConfig, } def get_config(environment="default"): """Get configuration based on the specified environment.""" return CONFIG[environment] ==> what to do for the production??
Brody
Brody11mo ago
not on railway it's not you are hardcoding your database url, please use environment variables
IssaKass
IssaKassOP11mo ago
how? can I get the environment variable from Railway?
Brody
Brody11mo ago
os.getenv in python, or something like that, please research that more carefully though
IssaKass
IssaKassOP11mo ago
so inside class ProductionConfig(Config), I add the psql url using env variables?
Brody
Brody11mo ago
you need to read the database url from the environment variable you have listed in railway
IssaKass
IssaKassOP11mo ago
ok after that?
Brody
Brody11mo ago
if done correctly, then your app should be connected to postgres
IssaKass
IssaKassOP11mo ago
I have a question
Brody
Brody11mo ago
yes?
IssaKass
IssaKassOP11mo ago
Is it good to have for example 3 services, one for Frontend (React+Vite), one for Backend (Flask), and one for DB (Postgresql)??
Brody
Brody11mo ago
yes that's absolutely the correct way
IssaKass
IssaKassOP11mo ago
But I have some issues, not knowing the env variables for example for frontend: VITE_APP_TITLE=My App VITE_API_URL=http://127.0.0.1:5000==> for development,
Brody
Brody11mo ago
and on railway you want to set VITE_API_URL to your backend domain
IssaKass
IssaKassOP11mo ago
I deployed it to Railway and getting: Throttling navigation to prevent the browser from hanging. See https://crbug.com/1038223. Command line switch --disable-ipc-flooding-protection can be used to bypass the protection==> and sometimes the route isn't opening, why??
Brody
Brody11mo ago
that would be a code issue, unfortunately I don't have a reason to give you as to why
IssaKass
IssaKassOP11mo ago
Developing locally, the error isn't happening
Brody
Brody11mo ago
that does not rule out a code issue does your frontend have a nixpacks.toml and a Caddyfile?
IssaKass
IssaKassOP11mo ago
yes
Brody
Brody11mo ago
okay then I've run out of ideas lol though that error is not specific to railway so please do some research on it
IssaKass
IssaKassOP11mo ago
I use Railway templates, and started coding
Brody
Brody11mo ago
I made that template so I can confirm it does not come with such errors
IssaKass
IssaKassOP11mo ago
I am uploading my backend (Flask) and I got CRASHED. With the following deployment logs: [2024-01-13 19:09:40 +0000] [7] [INFO] Starting gunicorn 20.0.4 [2024-01-13 19:09:40 +0000] [7] [INFO] Listening at: http://0.0.0.0:6148 (7) [2024-01-13 19:09:40 +0000] [7] [INFO] Using worker: sync [2024-01-13 19:09:40 +0000] [11] [INFO] Booting worker with pid: 11 Failed to find attribute 'app' in 'main'.
Brody
Brody11mo ago
can you share your repo?
Want results from more Discord servers?
Add your server