4

I am able to successfully deploy my Flask app on Heroku, but I'm not able to connect to my database (Internal Server Error). I noticed that the connection string in the Heroku DATABASE_URL begins with postgres:// instead of the usual postgresql://. I am able to connect to the Heroku database when I run my app locally using the connection string postgresql://[etc.] but not with postgres://[etc.]. Does anyone know how I can fix this? Heroku doesn't allow editing the DATABASE_URL environment variable. I've been pounding my head against the desk for hours and I'm about to give up.

Thank you.

1
  • How are you trying to use the connection string? Commented Mar 29, 2021 at 2:00

3 Answers 3

2

Do edit your connection string like this in python. I am using slicing. You can use similar approach in other language.

DATABASE_URI = os.environ['DATABASE_URL']

DATABASE_URI= DATABASE_URI[:8]+'ql' + DATABASE_URI[8:]

Essentially what it does is take the first 8 characters, adds 'ql' and then adds the remaining characters and replaces the string.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to recreate new environment variable with the same ids but with postgresql:// suffixe ?

3 Comments

No, please don't do this. The connection string can change at any time.
Any time ? Why not updating this env when the connection string change ? It's not supposed to change every day no ? Or maybe with the right buildpack the string will be ok
When it changes no notice is given: "The value of your app’s DATABASE_URL config var might change at any time. You should not rely on this value either inside or outside your Heroku app." If you set your own environment variable your application can break at any time without notice. It has nothing whatsoever to do with buildpacks.
0

replace postgres with postgresql

DATABASE_URL = os.environ['DATABASE_URL']
DATABASE_URL = str(DATABASE_URL).replace("postgres://", "postgresql://", 1)
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URL

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.