Connection refused when running mix ecto.setup command, packages installed with asdf - postgresql

I'm trying to follow the GraphQL tutorial.
I'm used asdf to install the erlang, elixir and postgres. I'm using a macbook.
When I get to the step to run mix ecto.setup, I'm getting the following error:
20:44:47.318 [error] GenServer #PID<0.1605.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
(db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Community.Repo couldn't be created: an exception was raised:
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
(db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
What it could be?

You have to start postgres, according to the asdf-postgree docs you have to:
pg_ctl start
And in order to run the commands mix ecto.create and mix ecto.setup you have to create a user postgres with the password postgres, and the postgres user must have permissions to both LOGIN and CREATEDB, then you can:
Create a default database:
createdb default
Log to that database, with:
psql -d default
And create the user with the permissions:
# CREATE ROLE postgres LOGIN CREATEDB PASSWORD 'postgres';

Related

mix ecto.create fails (** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) Ident authentication failed for user "postgres)

11:06:04.676 [error] GenServer #PID<0.290.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) Ident authentication failed for user "postgres"
(db_connection 2.4.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
(connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
11:06:04.695 [error] GenServer #PID<0.297.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) Ident authentication failed for user "postgres"
(db_connection 2.4.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
(connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Helloworld.Repo couldn't be created: killed
I already tried everything. I am on Fedora 35, i did all that the official Fedora PostgreSQL docs said. I changed the pg_hba.conf for this:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
I setted a password for the user postgres, i initialize it, and nothing happens.
I browsed through some 10 questions here on StackOverflow and none of the solutions works.
Any help?
Simply replace ident with md5 in pg_hba.conf and reload PostgreSQL, then you can use password authentication for localhost connections.

Elixir Phoenix ecto.create fail

i am following along with the Phoenix Up & Running guide at https://phoenixframework.readme.io/docs/up-and-running
and when I try to use ecto to create a database (Postgresql) I get the following error
mix ecto.create
17:55:35.631 [error] GenServer #PID<0.215.0> terminating
** (RuntimeError) Connect raised a CaseClauseError error. The exception details are hidden, as they may contain sensitive data such
as database credentials.
(postgrex 0.12.2) lib/postgrex/utils.ex:40: Postgrex.Utils.parse_version/1
(postgrex 0.12.2) lib/postgrex/protocol.ex:497: Postgrex.Protocol.bootstrap_send/4
(postgrex 0.12.2) lib/postgrex/protocol.ex:353: Postgrex.Protocol.handshake/2
(db_connection 1.1.3) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
(connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.11.2) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for HelloPhoenix.Repo couldn't be created: an exception was raised:
** (RuntimeError) Connect raised a CaseClauseError error. The exception details are hidden, as they may contain sensitive data such
as database credentials.
(postgrex 0.12.2) lib/postgrex/utils.ex:40: Postgrex.Utils.parse_version/1
(postgrex 0.12.2) lib/postgrex/protocol.ex:497: Postgrex.Protocol.bootstrap_send/4
(postgrex 0.12.2) lib/postgrex/protocol.ex:353: Postgrex.Protocol.handshake/2
(db_connection 1.1.3) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
(connection 1.0.4) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.11.2) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
The only suggestions I found was to update Postgrex. I ran mix deps.update postgrex and this did not fix it.
Finally I checked config/dev.exs and username/password are set to "postgres". I used PgAdmin3 to confirm this is a valid login.
So at this point I am stuck.
Ubuntu 19.10
postgres (PostgreSQL) 11.7 (Ubuntu 11.7-0ubuntu0.19.10.1)
installed from Ubuntu repo via apt-get
What are the versions of
Postgress and postgrex ?
postgrex supports PostgreSQL 8.4, 9.0-9.6, and later (hstore is not supported on 8.4)

mix ecto.create can't connect to Postgres even though Postgres is running and the credentials are correct

I created a new Phoenix project and checked the credentials in config/dev.exs, which are:
config :blog, Blog.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "blog_dev",
hostname: "localhost",
pool_size: 10
Now, the database blog_dev does not exist, but it's my (total beginner level) understanding that mix ecto.create should create it if it does not already exist. So I ran:
mix ecto.create
Which gave me the error
localhost:blog alex$ mix ecto.create
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted. Similar to atoms, keywords made exclusively of Unicode letters, numbers, underscore, and # do not require quotes
mix.exs:57
18:04:24.675 [error] GenServer #PID<0.212.0> terminating
** (DBConnection.ConnectionError) tcp recv: closed
(db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for Blog.Repo couldn't be created: an exception was raised:
** (DBConnection.ConnectionError) tcp recv: closed
(db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
This answer on StackOverflow suggests that this kind of error usually occurs when Postgres is not running. So I checked like so:
localhost:blog alex$ brew services list
Name Status User Plist
postgresql started
I also considered that my credentials were incorrect, so I tried manually logging into Postgres with the credentials in config/dev.exs like so:
localhost:blog alex$ psql postgres postgres -W
Password:
psql (11.3)
Type "help" for help.
postgres=#
The password I typed was 'postgres'. Does anyone have any suggestions about what could be going on here? Thanks!
A Postgres 11.4 pulled from docker and Phoenix 1.4.9 running on Elixir 1.8.2/OTP 21 work fine when the app is connecting to the default 5432 port.
docker pull postgres:11.4
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -d -p 5434:5432 postgres:11.4
mix archive.install hex phx_new 1.4.9
mix phx.new blog
mix ecto.create
# Compiling 13 files (.ex)
# Generated blog app
# The database for Blog.Repo has been created
Note that in the example above the local port 5434 is mapped to container's port 5432 where postgres listens. If we now change the mapping to 5434:5788, 5788 being a random port value not used by postgres, the setup breaks with an error similar to what you see.
docker container stop pg-docker
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=postgres -d -p 5434:5788 postgres:11.4
mix ecto.create
# 11:25:32.876 [error] GenServer #PID<0.217.0> terminating
# ** (DBConnection.ConnectionError) tcp recv: closed
# (db_connection) lib/db_connection/connection.ex:87: DBConnection.Connection.connect/2
# (connection) lib/connection.ex:622: Connection.enter_connect/5
# (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
# Last message: nil
# State: Postgrex.Protocol
# ** (Mix) The database for Blog.Repo couldn't be created: killed
Which means that your app tries establish a TCP connection to a closed port.
In other words, check your postgres config and make sure the port you are connecting to is the one where postgres listens.
Which gave me the error
localhost:blog alex$ mix ecto.create warning: found quoted keyword
"test" but the quotes are not required. mix.exs:57
The first thing you should do is fix all the warnings/errors that you are able to. So, what does your mix.exs file look like at line 57?
You need to do four things to create your database:
1) In mix.exs:
defp deps do [
...
...
{:ecto_sql, "3.0.3"}, # whatever versions you want here
{:postgrex, "0.14.1"},
{:phoenix_ecto, "~>4.0"}
]
2) In config/config.exs:
config :blog,
ecto_repos: [Blog.Repo]
3) In config/dev.exs:
config :blog, Blog.Repo,
database: "blog_dev",
username: "postgres",
password: "postgres",
hostname: "localhost",
port: "5432",
show_sensitive_data_on_connection_error: true,
pool_size: 10
You need to figure out what port your postgres server is running on. Mine (Postgres.app) runs on port 5432 by default.
3) In lib/blog/repo.ex:
defmodule Blog.Repo do
use Ecto.Repo,
otp_app: :blog,
adapter: Ecto.Adapters.Postgres
end
4) .../phoenix_apps/blog$ mix ecto.create

How can I connect to Postgres with mix command on Phoenix framework?

I am trying to run mix ecto.create on Phoenix project and got error:
00:30:34.332 [error] GenServer #PID<0.328.0> terminating
(RuntimeError) Connect raised a CaseClauseError error. The exception details are hidden, as they may contain sensitive data such as database credentials.
(postgrex) lib/postgrex/utils.ex:40: Postgrex.Utils.parse_version/1
(postgrex) lib/postgrex/protocol.ex:497: Postgrex.Protocol.bootstrap_send/4
(postgrex) lib/postgrex/protocol.ex:353: Postgrex.Protocol.handshake/2
(db_connection) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
(Mix) The database for Discuss.Repo couldn't be created: an exception was raised:
(RuntimeError) Connect raised a CaseClauseError error. The exception details are hidden, as they may contain sensitive data such as database credentials.
(postgrex) lib/postgrex/utils.ex:40: Postgrex.Utils.parse_version/1
(postgrex) lib/postgrex/protocol.ex:497: Postgrex.Protocol.bootstrap_send/4
(postgrex) lib/postgrex/protocol.ex:353: Postgrex.Protocol.handshake/2
(db_connection) lib/db_connection/connection.ex:135: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
My config/dev.exs
# Configure your database
config :discuss, Discuss.Repo,
adapter: Ecto.Adapters.Postgres,
username: "elixir",
password: "Ohtai3ai",
database: "discuss_dev",
hostname: "127.0.0.1",
pool_size: 10
My Elixir version is 1.6.6 (compiled with OTP 19)
Postgres 10
Out of date postgrex dependency could be the reason, try upgrading it.
mix deps.update postgrex
The solution comes from postgrex issue #370 which is easily found by googling the exact error message.
Resolved. Just update new versions for
phoenix_ecto 3.3.0
postgrex 0.13.5
ecto 2.2.10

Error when trying to create database - Ecto - Phoenix

I'm getting an error when trying to create a database. I have postgres installed and I've already made a few test projects successfully. And, I've not seen this error. Any help would be great:
ERROR:
~/Desktop/elixir/restore $ mix ecto.create
** (Mix) The database for Restore.Repo couldn't be created: tcp connect: connection refused - :econnrefused
21:52:23.978 [error] GenServer #PID<0.150.0> terminating
** (Postgrex.Error) tcp connect: connection refused - :econnrefused
(db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:623: Connection.enter_connect/5
(stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
I had same problem today when tried to run mix ecto.create.
So first make sure you have postgres. recommend using brew to install it.
brew install postgres
use brew services to start/stop postgresql,
create a database,
create postgres superuser,
probably you will need to give permission on folders.
these links were useful to me :
psql: FATAL: role "postgres" does not exist
psql: FATAL: database "<user>" does not exist
I had the same issue, and the problem was that postgres was not running.
so, to check this, you should run :
brew services list
and then, if you see :
Name Status User Plist postgresql stopped
you should run :
brew services start postgresql
cheers!