Error when trying to create database - Ecto - Phoenix - postgresql

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!

Related

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

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

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';

postgresql server failed to start after system upgrade

I upgrade debian server then I got this error:
[....] Starting PostgreSQL 9.1 database server: main[....] The PostgreSQL server
[FAILed to start. Please check the log output. ... failed!
failed!
But the log file is empty.
Can anyone help me to start PostgreSQL?
To solve the problem I changed the owner of the log file to postgres:
chown postgres:posgres logfile_path

eclipse luna and postgres db 9.4 connection

I am trying to connect postgres in eclipse. But I am getting below error after entering all details for jdbc connection:
Database:postgres
url:jdbc:postgresql://localhost:5432/postgres
user name:postgres
pass :postgres
jar file:D:\Workspace: postgresql-9.4-1202.jdbc41
Ping failed:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:427)
at