Rya

Rya

I’m trying to create a REST API following this guide: https://www.youtube.com/watch?v=LGY_eILc8Ks&list=PL2Rv8vpZJz4zM3Go3X-dda478p-6xrmEl&index=1.

I’m currently using:
Ubuntu 22.04 LTS
docker 24.0.7
elixir 1.17.2

Following the guide, i create and run a postgres container and try to create a project through the mix command.
I then configure the env.exs file as done in the second video.
At that point i run the mix ecto.create command, which fails with the following message:

`23:53:30.489 [error] Postgrex.Protocol (pid<0.6242.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

23:53:30.491 [error] :gen_statem pid<0.6242.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
(db_connection 2.7.0) lib/db_connection/connection.ex:104: DBConnection.Connection.handle_event/4
(stdlib 6.0.1) gen_statem.erl:3115: :gen_statem.loop_state_callback/11
(stdlib 6.0.1) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Queue: [internal: {:connect, :init}]
Postponed:
State: Postgrex.Protocol
Callback mode: &DBConnection.Connection.handle_event/4, state_enter: false

23:53:30.494 [error] Postgrex.Protocol (pid<0.6248.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

23:53:30.494 [error] :gen_statem pid<0.6248.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
(db_connection 2.7.0) lib/db_connection/connection.ex:104: DBConnection.Connection.handle_event/4
(stdlib 6.0.1) gen_statem.erl:3115: :gen_statem.loop_state_callback/11
(stdlib 6.0.1) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Queue: [internal: {:connect, :init}]
Postponed:
State: Postgrex.Protocol
Callback mode: &DBConnection.Connection.handle_event/4, state_enter: false
** (Mix) The database for Backend24h.Repo couldn’t be created: killed`

As far as i can tell by looking at other posts here this means there are no databases listening on the given port. But i can see and connect to the database by running psql in bash terminal opened in the container through a docker exec bash command, so the container is working properly

The videos i’ve linked don’t seem to imply there needs to be anything done besides creating and running the postgres container.

Am i doing something wrong or this linked to the fact that i use linux and not mac, some poor handling of docker, etc. ?

Showing Posts 1 to 5

dimitarvp

dimitarvp

In a Docker setup you don’t use localhost, you always have some made-up name like docker.dev or whatever. So find out the host and put it in your config.

arcanemachine

arcanemachine

Let’s see your Docker command. Are you exposing the ports correctly, e.g. -p 5432:5432?.

D4no0

D4no0

Even if postgres exposed, the localhost reference used is the container localhost not the host where postgres exposed the port. You need to use a different hostname if you want to access the database via the host: https://medium.com/@TimvanBaarsen/how-to-connect-to-the-docker-host-from-inside-a-docker-container-112b4c71bc66

My 2 cents is to not follow that guide and just find a docker-compose example script and use that, for example like this one: Docker Compose setup for Elixir, Phoenix, and Postgres · GitHub . It will make your life easier as to connect the database you can just use db as hostname, no further setup required.

Rya

Rya OP

So managed to make it work by following arcanemachine’s advice and re-running a container exposing the right port.

I wanna point out that the ecto.create command works with hostname: "localhost", despite the fact that is seemingly not the listed hostname of the container’s json you get when running docker inspect. I’m not quite sure i understand why that is but it does work.

My problem was also caused by renaming the databse in the dev.exs file, i thought it needed to match the container’s name but it does not.

So passing -p 5432:5432 to docker run and only modifying the username and password in the dev.exs file, while leaving other values as default, did the trick for me !

arcanemachine

arcanemachine

Glad to see that it helped… I know that I’m outclassed by @D4no0 and @dimitarvp but I just expose the ports as I described, and can reach them via localhost from outside the container.

I think it would probably be better (from a security and isolation standpoint) to expose the ports over an internal Docker network via -p 5432, then you could use the custom hostnames as described by the others, and avoid exposing the port on the host machine (I think?!). But I only use Docker for dev work personally, so I don’t mind exposing ports to the host machine.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews