How to Create A New App and Connect an Existing Database

I believe I’ve done this before and had no issues but now I’m getting stumped.

I have an app ‘wherecanwedance’ and a database ‘wherecanwedance-db’ and the app is having some weird JS & CSS issues so I wanted to troubleshoot by creating a new app and connect it to the existing Postgres DB (wherecanwedance-db).

The current site is deployed to http://www.wherecanwedance.com and the site I can’t get to connect to the same database is at https://wcwd5.fly.dev/.

I ran fly launch and created the new app name: wcwd5 (my fifth time trying this)

I create a new fly.toml, and did not create a database, and I created a new Upstash redis.

After the app was created I ran fly postgres attach wherecanwedance-db and that was successful but when I deploy the new app, it loads but all the DB tables are empty.

I tried detaching wherecanwedance-db from wherecandance but thats not working.

One weird thing is that when I run fly postgres list I get a list of wcwd2-db, wcwd3-db, and wherecanwedance-db (the 1st two where I tried to create a DB volumes snapshot of the wherecanwedance-db and restore it on a new DB, but my new deployed app is still coming with empty tables.

Am I missing something to get a new app running that is connected to an existing DB? I tried looking at some docs and I’m not sure if I should be using the legacy Postgres steps or something else. Thanks in advance!

Hi,

It’s subtle but when you use that command fly postgres attach db it creates a new database (for the app it is connected to): Attach or Detach a Fly App · Fly Docs

That’s why you are seeing that all the tables are empty. It’s a new database. Same app, but a different database. It’s kind of confusing because the word “database” (or “DB”) could be used for both the database app and the, er, database :slightly_smiling_face:

So to connect to an existing database (one that’s been created as a result of its database app being attached to a, er, compute app, for want of a better word) one option is to use its DATABASE_URL. That is added as a secret as part of the attach command. It’s how the “compute” app knows which database app to connect to and what credentials to use. It’s just done behind the scenes. That is stored as a secret. So it’s encrypted. How do you see what it is? :thinking: Well handily the compute app has to see its value, so you can too. SSH in to the compute app (using fly ssh console run from the folder the fly.toml is in). You get shell access to the VM. Now simply run echo $DATABASE_URL You can now see what the VM sees (what Fly added). The full connection string. That will include a username, password and database name. Armed with that, you can connect to the existing database it was connected to. And see all the tables, now with data and not empty :slightly_smiling_face:

Of course to connect from outside of the Fly private network, you will need e.g Wireguard. As of course your local PC can’t resolve app-name.internal domains.

Thanks for the reply Greg!

Your response helped point me in the right direction!

I was able to find this article about migration concerning the DATABASE-URL. It showed me another way to get the DATABASE_URL as well as the REDIS_URL and how to set that and deploy that to the new app and it worked!

I will share the link to the article here just in case anyone else happens to stumble across this issue in the future.

See Article

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.