The database for Api.Repo couldn't be created

So, I’m trying to setup the database that’s running on a container, when I run mix ecto.setup I started to receiving this error

** (Mix) The database for Api.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

I had tried all the points addressed by the message and all the solutions that I found searching about this error.

This is my config/dev.exs config

# Configure your database
config :api, Api.Repo,
  username: System.get_env("DATABASE_USER") || "postgres",
  password: System.get_env("DATABASE_PASS") || "postgres",
  database: System.get_env("DATABASE_NAME") || "api_dev",
  hostname: System.get_env("DATABASE_HOST") || "localhost",
  port: System.get_env("DATABASE_PORT") || 5432,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

and my docker-compose.yml

version: "3.7"

services:
  postgres-development:
    image: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "postgres"
    volumes:
      - $HOME/docker/volumes/postgres:/var/lib/postgresql/data

The strangest part is that yesterday I run all ecto commands and they all was succeeds and today when I tried to runned a simple migration command I started to receive this error, and I don’t edited none configuration file.

1 Like

I suggest checking docker container logs for warning or errors. Most likely either that is either network configuration issue or database. At least I do no see matching database name “api_dev” in container configuration. Ensure that database is available on localhost:5432.

1 Like

The mix ecto.setup should create migrate and seed the database, and yes I already checked the running container

1 Like

Can you verify that you can manually connect to the database with psql from the same place you’re attempting to run the mix tasks?

1 Like

I didn’t tried that, but using table plus I was able to access the container

If you’re not trying to access it in the exact same way that mix would be attempting to access it (from local, from inside container, etc), then you’re going to be stuck. Another option would be to run iex and try to connect to postgresql from there. I’d try to do either of those as your next debug step.

I tried to run inside iex but I receive [error] Postgrex.Protocol (#PID<0.495.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): timeout, I’m using the same parameters to connect throu

Hmmm…now you’re just going to have to guess. It is a timeout, so it looks like iex isn’t seeing the port being open at all. Could be that localhost isn’t translating properly? Maybe try 127.0.0.1 instead?

So… I installed the Postgres.app and it work, probably my docker is broken

1 Like

Highly unlikely that Docker is broken. You either didn’t specify a host name or Docker is listening on another IP address as @tj0 said. Pretty sure your scenario is super easy to handle, shame that you gave up.

My Postgres is running on 5432 and docker as well, the hostname it’s localhost, and I noticed that some colegues is getting the same error using a totally different docker-compose.yml or simply by running the Postgres container that docker desktop give us.

I runned another test, I take a fresh new MacBook Pro, installed the docker and executed the Postgres container, and worked, so probably is some bad config on my docker that is causing this