How to get DATABASE_URL if missing

I know there is DATABASE_URL is set in my app secret after fly pg create.
Can I re-get DATABASE_URL if happen to unset DATABASE_URL or any other reason to requie DATABASE_URL ?I found fly secret only give digest.

2 Likes

Ah, well that DATABASE_URL secret is not set initially (after the fly pg create). The credentials shown then are your superuser ones. The output says that string will only be shown once and to make a note of it.

It is the subsequent step, attaching the postgres to an app, that triggers the DATABASE_URL to be set. You can see in the docs you can attach and detach the database which will set/unset that value:

If you want to get the current value, as you say you can’t (externally) see it because it is encrypted (like all secrets are). That’s a good thing. However your app (the vm) can get its value (naturally - else you couldn’t use it in your code!). So if you need to get its current value you would have to output it from your app to somewhere you could see it. Be careful doing that, naturally.

1 Like

Thank you! So when I want to get DATABASE_URL even if lose DB connection, I need to re-attach postgres through cli to get DATABASE_URL ?

Well losing a DB connection has different meanings, but if you detach your database from the app, I assume that will cause the connection to be lost. If you were to then re-attach it, it would set a new value for DATABASE_URL.

Since that would change the app’s environment variables (secrets), the app would then need to be re-deployed in order for that change to be applied. Else your code would still be using the old value of DATABASE_URL. I haven’t tried detaching and attaching a database like this so not sure if that happens automatically. You may want to wait for someone from Fly to confirm, or try on a staging database first.

Yeah as @greg mentioned, losing a db doesn’t necessarily mean that the db is detached so the DATABASE_URL may remain unchanged. If you do run into issues and it seems to have been changed, you could re-run the attach command and it should update DATABASE_URL

@shortdiv I want to know how to get DATABASE_URL again. If can’t, it means only I can do detach and attach to get DATABASE_URL ?

As @greg mentioned, you could write the DATABASE_URL to stdout to get the value but you’d want to do this as discreetly as you can so you don’t risk exposing that publicly.

@shortdiv I understand DATABASE_URL is secret. Thank you. But my question is how to get that again. I want to confirm detach and attach is a workaround to get DATABASE_URL.

@shishi attach creates a new database url. Technically, it creates a new database (unless you override the db name option), user, and password. Then it generates a URL from that and sets it as a secret in your app.

In Fly.io language, a “secret” means a thing that you cannot retrieve (neither can we!).

If you unset DATABASE_URL, it’s gone forever. Does that makes sense?

@kurt Yes. I understand if I forget to note DATABASE_URL when I attach db and then If I need raw DATABASE_URL, I can only do re-attach and get New DATABASE_URL.

@kurt @greg @shortdiv Thank you very much.

1 Like

If you still have the secret set, you can run fly ssh console to connect to your app, then echo $DATABASE_URL to see it. If you ran fly secrets unset ... that won’t work.

3 Likes

@kurt This is exactly what I wanted to know. Thank you very much