Cannot attach postgres to app (Oops something went wrong)

I have an app named yuubot and postgres app named yuubot-db.
I tried

flyctl postgres attach --app yuubot yuubot-db

it tells me “Oops, something went wrong! Could you try that again?”
How should I resolve this?

Also, how could I obtain the DATABASE_URL for yuubot to connect to postgres after attaching? (Or could I just simply use the Connection string shown when creating the database)

Thanks for the help.

What happens when trying again? If that continues failing, try running with LOG_LEVEL=debug. That will hopefully show some more info about what went wrong.

The attach commands does a few things. One is creating a new db user (though it will skip this if a previous attach already did that) and then setting the DATABASE_URL in the target app.

The problem resolved itself today, the database is now attached to the app.
It keeps failing even retried many times yesterday, maybe a simple reboot will do the work.

Now it has added DATABASE_URL as a secret to the app, how should I define it in my code?
Previously I use

DATABASE_URL = os.environ['DATABASE_URL']

Is there something similar in fly.io?

That should work. Running fly pg attach will set DATABASE_URL as a secret, which gets set as an environment variable in the app.