Add Postgres To Flask
I have some truble connecting Postgresql to Flask and deploying it to Railway App
42 Replies
Project ID:
c6894929-c040-4842-8c83-4516d57a14fe
c6894929-c040-4842-8c83-4516d57a14fe
can anyone help me?
have you added the database variable references to your backend? https://docs.railway.app/guides/variables#referencing-another-services-variable
I am new to deploying, its my first app that I will deploy it, it is built with Flask+db+React
okay that's cool, but that doesn't quite answer the question
I added this one only to the backend:
could you also provide what to add in .env for flask?
you don't need an .env file
configuration? for example config.py
this looks good to me, are you using that environment variable in your code
yes: FLASK_APP=main
FLASK_ENV=development
FLASK_DEBUG=True
I'm sorry but that doesn't answer the question
no
Just added it to Railway
you need to use the DATABASE_URL environment variable in code when connecting to the database
How?
that would more so be a question for the postgres client you are using in your code
haven't understood your phrase
...
you need to read the documentation for the database client you are using in code
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??
not on railway it's not
you are hardcoding your database url, please use environment variables
how?
can I get the environment variable from Railway?
os.getenv in python, or something like that, please research that more carefully though
so inside class ProductionConfig(Config), I add the psql url using env variables?
you need to read the database url from the environment variable you have listed in railway
ok
after that?
if done correctly, then your app should be connected to postgres
I have a question
yes?
Is it good to have for example 3 services, one for Frontend (React+Vite), one for Backend (Flask), and one for DB (Postgresql)??
yes that's absolutely the correct way
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,
and on railway you want to set VITE_API_URL to your backend domain
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??
that would be a code issue, unfortunately I don't have a reason to give you as to why
Developing locally, the error isn't happening
that does not rule out a code issue
does your frontend have a nixpacks.toml and a Caddyfile?
yes
okay then I've run out of ideas lol
though that error is not specific to railway so please do some research on it
I use Railway templates, and started coding
I made that template so I can confirm it does not come with such errors
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'.
can you share your repo?