Unable to connect to Postgresql database

I want to build my project and run my tests.

The project is using Elixir/Phoenix.

This passes:

cd phoenix-chat-api
mix local.rebar --force
mix local.hex --force
mix deps.get

So it gets the dependencies but when it gets to the next line:

mix test

It has this error

==> postgrex
Compiling 61 files (.ex)
Compiling lib/postgrex/protocol.ex (it's taking more than 10s)
Compiling lib/postgrex/default_types.ex (it's taking more than 10s)
Generated postgrex app
==> ecto
Compiling 69 files (.ex)
Compiling lib/ecto/query/builder/lock.ex (it's taking more than 10s)
Compiling lib/ecto/repo/queryable.ex (it's taking more than 10s)
Compiling lib/ecto/query/inspect.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/filter.ex (it's taking more than 10s)
Compiling lib/ecto/repo/preloader.ex (it's taking more than 10s)
Compiling lib/mix/tasks/ecto.gen.repo.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/mysql.ex (it's taking more than 10s)
Compiling lib/ecto/schema.ex (it's taking more than 10s)
Compiling lib/ecto/migration/runner.ex (it's taking more than 10s)
Compiling lib/ecto/repo/schema.ex (it's taking more than 10s)
Compiling lib/ecto/embedded.ex (it's taking more than 10s)
Compiling lib/ecto/migration/schema_migration.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/preload.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/order_by.ex (it's taking more than 10s)
Compiling lib/ecto/type.ex (it's taking more than 10s)
Compiling lib/ecto/uuid.ex (it's taking more than 10s)
Compiling lib/ecto/association.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/postgres/connection.ex (it's taking more than 10s)
Compiling lib/ecto/query/planner.ex (it's taking more than 10s)
Compiling lib/mix/tasks/ecto.gen.migration.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/join.ex (it's taking more than 10s)
Compiling lib/ecto/log_entry.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/select.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/distinct.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/limit_offset.ex (it's taking more than 10s)
Compiling lib/ecto/changeset.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/group_by.ex (it's taking more than 10s)
Compiling lib/ecto/changeset/relation.ex (it's taking more than 10s)
Compiling lib/ecto/multi.ex (it's taking more than 10s)
Compiling lib/ecto.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/postgres.ex (it's taking more than 10s)
Generated ecto app
==> phoenix_ecto
Compiling 4 files (.ex)
Generated phoenix_ecto app
==> phoenix_chat
Compiling 19 files (.ex)
Generated phoenix_chat app
** (Mix) The database for PhoenixChat.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:148: DBConnection.Connection.connect/2
        (connection) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
21:05:31.639 [error] GenServer #PID<0.3286.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
    (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Build failed !!!.

So do I need to install Postgresql?

In the pipeline action I clicked “services” and added a postgres service.

Then it showed a connection example:

 # You need to install Postgres client in Packages & Setup tab to connect 
 PGPASSWORD=qwerty psql -h postgres -U postgres 

So i pasted that example into “Packages and setup commands”

Then when running the pipeline I get:

Build started.
Clearing caches.
Fetching changes started.
Fetching changes finished.
Creating image started.
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM library/elixir:1.3
 ---> 83741b364255
Step 2 : RUN PGPASSWORD=qwerty psql -h postgres -U postgres
 ---> Running in 46f242d5538b
e/bin/sh: 1: psql: not found
eRemoving intermediate container 46f242d5538b
The command '/bin/sh -c PGPASSWORD=qwerty psql -h postgres -U postgres' returned a non-zero code: 127
Build failed !!!.

Am I on the right track? How do I instal postgresql? I tried this

eg:

apt-get update -y
apt-get install -y postgresql

But it didn’t work - it is an Ubuntu command - are all Buddy servers on Ubuntu?

I’m on a mac and to install postgresql for me it was brew install postgresql

How do I install postgresql for my Elixir/Phoenix project?

Hi Ben,
You are right - you need to install Postgresql in the main image. You can check the OS looking up the Docker files on Docker hub: Elixir was created on the basis of Erlang, which is based on Jessie Debian. Following the instructions from https://wiki.debian.org/PostgreSql the installation should look like this:

apt-get install -y postgresql postgresql-client