psycopg2.OperationalError while migrating PostgreSQL database
parthkotwal
TRIALOP

9 months ago

I’m trying to deploy my Flask app on the free tier with a PostgreSQL database. However, after running migrations both locally and in the Railway CLI, the application fails to load on Railway with the error message:

"Application failed to respond".

When I try to run migrations locally, I do not face any errors since I locally store and use DATABASE_URL=[value of DATABASE_PUBLIC_URL] variable of my database. When using the CLI, my DATABASE_URL variable's value is the internal Railway URL to the database (DATABASE_URL). When I try to run migrations on the CLI, I receive this error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "postgres.railway.internal" to address: nodename nor servname provided, or not known

I’ve verified that all dependencies are installed and up-to-date. There's also no message in the railway logs, I only see the error when I try migrating. I’ve tried deleting the migrations/ directory locally and re-running flask db init, followed by the migration steps again, but this didn’t solve the issue. Here is my app.py :

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from extensions import db
import os
from dotenv import load_dotenv

load_dotenv()

def create_app(config_class=None):
    app = Flask(__name__)
    if config_class:
        app.config.from_object(config_class)

    app.config['SECRET_KEY'] = os.environ.get("FLASK_SECRET_KEY")

    database_url = os.getenv("DATABASE_URL")
    
    if database_url:
        if database_url.startswith("postgres://"):
            database_url = database_url.replace("postgres://", "postgresql://", 1)
        app.config['SQLALCHEMY_DATABASE_URI'] = database_url
    else:
        # Default to SQLite if no DATABASE_URL is set
        app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///app.db"

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    migrate = Migrate(app, db)

    db.init_app(app)

    from routes import routes
    app.register_blueprint(routes, url_prefix="/")
        
    return app

if __name__ == "__main__":
    app = create_app()
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000)), debug=False)
Solved

5 Replies

brody
EMPLOYEE

9 months ago

Hello,

Running migrations via the CLI will run them locally as we don't yet provide a way to run them on the service itself.

Thus, you need to use the database public host and port when running commands locally.

Best,

Brody


Status changed to Awaiting User Response Railway 9 months ago


brody

Hello,Running migrations via the CLI will run them locally as we don't yet provide a way to run them on the service itself.Thus, you need to use the database public host and port when running commands locally.Best,Brody

parthkotwal
TRIALOP

9 months ago

Oh ok that makes sense! So my database was migrated properly but the 502 Error is unrelated? Thank you so much


Status changed to Awaiting Railway Response Railway 9 months ago


brody
EMPLOYEE

9 months ago

Looks like you have solved the 502 error?


Status changed to Awaiting User Response Railway 9 months ago


parthkotwal
TRIALOP

9 months ago

Yes just changed my start command.


Status changed to Awaiting Railway Response Railway 9 months ago


brody
EMPLOYEE

9 months ago

Awsome!


Status changed to Awaiting User Response Railway 9 months ago


Status changed to Solved brody 9 months ago


Loading...