Database Connection Failed

I’m installing phoenix, for now, I have a problem to connect on my local database which is PostgreSQL when I run mix ecto.create this is the error I get:

Compiling 14 files (.ex)
Generated demo app

23:12:59.230 [error] GenServer #PID<0.374.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
    (db_connection 2.2.2) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
    (connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Demo.Repo couldn't be created: killed

Here is the config/dev.exs file the part of the database:

username: "mucort",
database: "demo",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10

I just removed the password field here

I’m connecting to it in my terminal it works:

psql --dbname=demo --username=mucort --host="localhost" --port=5432 --password

Please I need your help

Are you running your application in a container? Or natively on the same host as the database, without any containerisation or virtualization in-between?

Yes it is natively no containerisation or virtualization in-between

Show us your Demo.Repo source? It should look something like this:

defmodule Demo.Repo do
  use Ecto.Repo,
    otp_app: :demo,
    adapter: Ecto.Adapters.Postgres

@dimitarvp the content in lib/demo_app/repo.ex:

defmodule DemoApp.Repo do
  use Ecto.Repo,
    otp_app: :demo_app,
    adapter: Ecto.Adapters.Postgres

I think you’ll have to work on your pg_hba.conf file but not sure. Make certain that you’re allowed and trusted not only through domain sockets but network connections as well.

1 Like

Ecto by default use TCP to connect on postgresql and it allows to use a unix socket

in dev.exs remove hostname and add:

socket_dir: "/var/run/postgresql"

Glad it works for you on dev, I just prefer to change the security settings of PG a little. :slight_smile:

1 Like