New Elixir app: problem connecting PostgreSQL db

I’m still trying to get my first Fly app up and running. When I created the app with fly launch, I created a PostgreSQL db as well. Now, when I try to deploy my app, I get an error during the database migration:

$ fly deploy
[...]
	 Starting virtual machine
	 Starting init (commit: 249766e)...
	 Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
	 no label, UUID=27602f5f-3d63-413e-9103-6c38ccb98510
	 Preparing to run: `/app/bin/migrate` as nobody
	 2022/10/27 16:48:39 listening on [fdaa:0:c77d:a7b:276d:a79:3d5c:2]:22 (DNS: [fdaa::3]:53)
	 16:48:41.311 [error] Postgrex.Protocol (#PID<0.136.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (top2.nearest.of.shivelight-db.internal:5432): non-existing domain - :nxdomain
	 16:48:41.311 [error] Postgrex.Protocol (#PID<0.137.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (top2.nearest.of.shivelight-db.internal:5432): non-existing domain - :nxdomain
	 16:48:41.311 [error] Postgrex.Protocol (#PID<0.136.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (top2.nearest.of.shivelight-db.internal:5432): non-existing domain - :nxdomain
	 16:48:41.311 [error] Postgrex.Protocol (#PID<0.137.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (top2.nearest.of.shivelight-db.internal:5432): non-existing domain - :nxdomain
	     indexes with a migration_lock)
	 "mix ecto.create". Alternatively you may configure Ecto to use
	       migration_source: "some_other_table_for_schema_migrations",
	 See DBConnection.start_link/2 for more information
	     indexes with a migration_lock)
	 "mix ecto.create". Alternatively you may configure Ecto to use
	       migration_source: "some_other_table_for_schema_migrations",
	 See DBConnection.start_link/2 for more information
	     (ecto_sql 3.9.0) lib/ecto/adapters/sql.ex:905: Ecto.Adapters.SQL.raise_sql_call_error/1
	     (ecto_sql 3.9.0) lib/ecto/adapters/sql.ex:997: Ecto.Adapters.SQL.execute_ddl/4
	     (ecto_sql 3.9.0) lib/ecto/migrator.ex:146: Ecto.Migrator.with_repo/3
	     nofile:1: (file)
	     (ecto_sql 3.9.0) lib/ecto/adapters/sql.ex:905: Ecto.Adapters.SQL.raise_sql_call_error/1
	     (ecto_sql 3.9.0) lib/ecto/adapters/sql.ex:997: Ecto.Adapters.SQL.execute_ddl/4
	     (ecto_sql 3.9.0) lib/ecto/migrator.ex:146: Ecto.Migrator.with_repo/3

I don’t know if this is permissions-related, but the DATABASE_URL is set, and I assume that it is correct since it was set by the launch command.

In what may be a related issue, I also can’t connect to the Postgres DB:

ian@alder $ fly postgres connect
Error app myapp is not a postgres app

I don’t know what “is not a postgres app” signifies here. Certainly, there is a Postgres DB running, and when I tried fly attach, it says I’m already done:

ian@alder $ flyctl postgres attach myapp-db --verbose
Error consumer app "myapp" already contains a secret named DATABASE_URL

I am seeing the exact same issue. Unfortunately I haven’t figured out yet what the issue is. Have you had any luck in resolving the problem?

This is happening because you’re most likely inside your app folder so fly.toml will be used to get the app name. Try fly postgres connect -a shivelight-db

Are you passing socket_options: [:inet6] on your ecto config? Example:

@bzuu I’m afraid I took the coward’s way out … I deleted the main app (not PostgreSQL), and re-created it. This time, it connected OK.

So: on the plus side, I now have an app that’s connected to PostgreSQL OK. On the minus side, I still don’t know what went wrong the first time, and that’s not a comfortable feeling.

@lubien Yes, connecting the console to the PostgreSQL app does work with the -a param. Obvious in retrospect really.

As a suggestion, I think what might help is some overview of the conceptual model: there’s lots of great docs on the steps to take to do things with flyctl, but I found it’s taking me some time and effort to grok what’s happening under the hood, what the moving pieces are, etc.

1 Like

@ijdickinson thanks for your feedback on this, glad to know it’s running properly now. Our fly launch should do all the work needed to set up one’s phoenix app, let us know if something like this happens.

@bzuu since you bumped into the same issue, can you take a look at my previous message and see if it’s helpful?

@ijdickinson and @lubien thanks for your responses. I had tried several times indeed, but no luck so far. I’ll definitely try your suggestion, but am not near my machine at the moment. Will post an update when I get a chance to try it.

1 Like

OK, took a while to figure this out (I am still very new to Elixir/Phoenix as well as Fly…), but finally found the issue.

I am using a Visual Studio devcontainer image for Elixir and Phoenix, which apparently by default comes with a very old phx.new version (1.6.2). After upgrading to the latest version (1.6.15) everything works as expected. It does indeed include the IPV6 change @lubien suggested.

When I only made that change however in the 1.6.2 generated files it still did not work although it seemed to get a bit further in the process.

Thanks for the help!

1 Like