mbski

mbski

Setting up Elixir with Dockerized PostgreSQL

Hello,

I am coming back to Elixir world after one year and I wanted to learn also new stuff so I decided to use Docker to my development.

I pulled pgadmin4 and postgresql, set it up according to documentation/articles. It looks like it works fine. I can log in into pgadmin4 through docker, I can open server with credentials given for postgresql container.

Elixir works fine if I want to play with localhost - I have also desktop version of PGAdmin4 and if I use credentials and localhost I can easily create ecto database.

Problem starts when I want to use locally developed Elixir/Phoenix app with PostgreSQL which is in container. I couldn’t find anything that could have helped me resolved this.

Container name: postgres_db,
Username: postgres,
Password: admin123,
Image: postgres

dev.exs

config :weather, Weather.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "postgres_db",
  port: 5432,
  database: "weather_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

Rest left unchanged from initially created files.

PS F:\weather> mix ecto.create
Compiling 28 files (.ex)
Generated weather app

08:05:39.563 [error] Postgrex.Protocol (#PID<0.416.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres_db:5432): non-existing domain - :nxdomain
** (Mix) The database for Weather.Repo couldn’t be created: connection not available and request was dropped from queue after 2000ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:

  1. Ensuring your database is available and that you can connect to it
  2. Tracking down slow queries and making sure they are running fast enough
  3. Increasing the pool_size (although this increases resource consumption)
  4. Allowing requests to wait longer by increasing :queue_target and :queue_interval

See DBConnection.start_link/2 for more information

Thats what i got when i try to create database.

I also tried to use IP address that I put while creating server in postgres, but it still times out.

My another try was also using docker-compose.yml

version: '3.8'

services:
  postgres_db:
    image: postgres
    volumes:
      - /var/lib/postgresql/data
    container_name: weather
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: weather_dev
    ports:
      - 5432:5432

Accordingly I updated dev.exs to fit those variables and stopped container created meanually (to omit port conflicts). Effect remains the same. Its not possible to connect Elixir with dockerized Postgres and I cant create database for my app.

Thanks for any help :wink:

Marked As Solved

Tyson

Tyson

Most common cause of mismatched credentials when trying to connect to PG in container is that you still have PG running on the host system and are actually connecting to that. Either shut down PG on the host and restart the container, or bind the container port to something other than 5432 (and then configure Ecto to use the new port).

Also Liked

adamu

adamu

For local development I like to use this compose file as a template, which works well with an out of the box phoenix project.

services:
  postgres:
    image: postgres:17
    container_name: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
      - type: volume
        source: data
        target: /var/lib/postgresql/data
    ports:
      - name: postgres
        target: 5432
        published: 5432

volumes:
  data:
D4no0

D4no0

Is your elixir container part of that docker-compose too? If yes your configuration should work, otherwise you need to specify a custom network, as only containers from the same docker-compose are part of the same network automatically.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement