Elixir-Phoenix: mix ecto.create and mix phx.server provoke same error but contain different postgres db parameters - postgresql

When running mix ecto.create I get the following error:
17:47:14.118 [error] GenServer #PID<0.299.0> terminating
** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) no pg_hba.conf entry for host "10.110.0.3", user "staging", database "postgres", SSL off
(db_connection 2.4.0) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
(connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
(stdlib 3.15.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
Then, if I try to run mix phx.server I get the following error:
[error] Postgrex.Protocol (#PID<0.358.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) no pg_hba.conf entry for host "10.110.0.3", user "staging", database "contact", SSL off
My database config in dev.exs is:
config :contact_api, ContactApi.Repo,
username: System.get_env("DB_USER") || "postgres",
password: System.get_env("DB_PWD") || "postgres",
database: System.get_env("DB_NAME") || "contact",
hostname: System.get_env("DB_HOST") || "localhost",
port: System.get_env("DB_PORT") || "5432",
show_sensitive_data_on_connection_error: true,
pool_size: 10
I have already checked that env variables are properly setter.
I have also tried to test different env values but mix ecto.create still shows postgres for the database name at the error trace.
I can connect to the database using psql from the same server as the app.
When running a local DB with docker; mix ecto.create, mix ecto.migrate, mix test, and mix phx.server run as expected.
Any help will be much appreciated!

I have solved my problem. I am working with a cloud managed postgres database that is hosted by Digital Ocean. The only connections allowed are encrypted ones using ssl certificates. Error trace just informs about no user entry in pg_hba.conf but no reference to the ssl issue. Hence I had to modify config/dev.exs:
config :contact_api, ContactApi.Repo,
username: System.get_env("DB_USER") || "postgres",
password: System.get_env("DB_PWD") || "postgres",
database: System.get_env("DB_NAME") || "contact",
hostname: System.get_env("DB_HOST") || "localhost",
port: System.get_env("DB_PORT") || "5432",
show_sensitive_data_on_connection_error: true,
pool_size: 10
ssl: true,
ssl_opts: [
cacertfile: "cert/ca-certificate.crt"
]

Related

GAE Connection to SQL: password authentication failed for user 'postgres'

nodejs app on GAE flex deploys correctly, but won't connect to postgres, even though initial knex migration worked and the tables were created. Ive read through the documentation and cant understand how all of the below can be true.
running psql -h [ipaddress] -p 5432 -U postgres mydb and entering the password from my local machine works!
package.json..
"prestart": "npx knex migrate:latest && npx knex seed:run
"start": "NODE_ENV=production npm-run-all build server"
worked! tables were created and seed was run
knexfile
production: {
client: 'postgresql',
connection: {
database: DB_PASS,
user: DB_USER,
password: DB_PASS,
host: DB_HOST
},
pool: {
min: 2,
max: 10
},
migrations: {
directory: './db/migrations',
tableName: 'knex_migrations'
},
seeds: {
directory: './db/seeds/dev'
}
}
yaml
runtime: nodejs
env: flex
instance_class: F2
beta_settings:
cloud_sql_instances: xxxx-00000:us-west1:myinst
env_variables:
DB_USER: 'postgres'
DB_PASS: 'mypass'
DB_NAME: 'myddb'
DB_HOST: '/cloudsql/xxxx-00000:us-west1:myinst'
handlers:...
IAM
oddly, it was a log only issue. the logs still say user authentication failed, but actually the app was connected.

Error creating a connection to Postgresql while preparing konga database

After installing Konga, we are trying to prepare Konga database on the already running Postgresql database. by using suggested command i.e.
node ./bin/konga.js prepare --adapter postgres --uri postgresql://localhost:5432/konga
But we are facing the error as below:
Error creating a connection to Postgresql using the following settings:
postgresql://localhost:5432/konga?host=localhost&port=5432&schema=true&ssl=false&adapter=sails-postgresql&user=postgres&password=XXXX&database=konga_database&identity=postgres
* * *
Complete error details:
error: password authentication failed for user "root"
error: A hook (`orm`) failed to load!
error: Failed to prepare database: error: password authentication failed for user "root"
We even created the schema konga_database manually and have tried several variations for prepare command but no fate
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong:XXXX#localhost:5432/konga_database
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga_database
Below is config/connections.js
postgres: {
adapter: 'sails-postgresql',
url: process.env.DB_URI,
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'XXXX',
port: process.env.DB_PORT || 5432,
database: process.env.DB_DATABASE ||'konga_database',
// schema: process.env.DB_PG_SCHEMA ||'public',
// poolSize: process.env.DB_POOLSIZE || 10,
ssl: process.env.DB_SSL ? true : false // If set, assume it's true
},
Below is .env file configuration
PORT=1337
NODE_ENV=production
KONGA_HOOK_TIMEOUT=120000
DB_ADAPTER=postgres
DB_URI=postgresql://localhost:5432/konga
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=XXXX
KONGA_LOG_LEVEL=info
TOKEN_SECRET=
kong and postgresql are already running on the AWS linux AMI 2 server on there respective ports i.e. 8443 & 5432
Please help us to prepare DB and start konga service. Also. let us know in case you need more info.
Node v: v12.19.0
NPM v: 6.14.8
Regards
Nitin G
Maybe I overlooked it, but what version of PostreSQL are you using?
Konga is not able to support postgresql 12:
https://github.com/pantsel/konga/issues/487
Have you tried like this?
.env
DB_URI=postgresql://userdb:passworddb#localhost:5432/kongadb
I tried on Postgresql 9.6
https://www.rosehosting.com/blog/how-to-install-postgresql-9-6-on-ubuntu-20-04/

Heroku can't connect with Postgres DB/Knex/Express

I have an Express API deployed to Heroku, but when I attempt to run the migrations, it throws the following error:
heroku run knex migrate:latest Running knex migrate:latest on ⬢
bookmarks-node-api... up, run.9925 (Free) Using environment:
production Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
In my knexfile.js, I have:
production: {
client: 'postgresql',
connection: {
database: process.env.DATABASE_URL
},
pool: {
min: 2,
max: 10
},
migrations: {
directory: './database/migrations'
}
}
I also tried assigning the migrations directory to tableName: 'knex_migrations' which throws the error:
heroku run knex migrate:latest Running knex migrate:latest on ⬢
bookmarks-node-api... up, run.7739 (Free) Using environment:
production Error: ENOENT: no such file or directory, scandir
'/app/migrations'
Here is the config as set in Heroku:
-node-api git:(master) heroku pg:info
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: 0/20
PG Version: 10.7
Created: 2019-02-21 12:58 UTC
Data Size: 7.6 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
I think the issue is that for some reason, it is looking at localhost for the database, as if the environment is being read as development though the trace shows Using environment: production.
When you provide an object as your connection you're providing individual parts of the connection information. Here, you're saying that the name your database is everything contained in process.env.DATABASE_URL:
connection: {
database: process.env.DATABASE_URL
},
Any keys you don't provide values for fall back to defaults. An example is the host key, which defaults to the local machine.
But the DATABASE_URL environment variable contains all of the information that you need to connect (host, port, user, password, and database name) in a single string. That whole value should be your connection setting:
connection: process.env.DATABASE_URL,
You should check to see if the Postgres add-on is setup as described in these docs since the DATABASE_URL is automatically set for you as stated here.

How to disconnect Postgrex connection?

I'm trying to figure out how to connect to a postgres database, run a query, and then disconnect.
Looking at Postgrex, I establish a connection using
{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")
Then I execute my query using
Postgrex.query!(pid, "SELECT user_id, text FROM comments", [])
But then, how do I disconnect?
I'd like to disconnect because I am looping through N databases and running the same query on all of them.
I tried exiting the Process e.g. Process.exit(pid, :bye), but it kills the spawning process also because it's started with start_link/3. I can't find a start/3 function in Postgrex.
As the pid returned by Postgrex.start_link is a GenServer, you can use GenServer.stop/1:
iex(1)> {:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")
{:ok, #PID<0.277.0>}
iex(2)> GenServer.stop(pid)
:ok
iex(3)> GenServer.stop(pid)
** (exit) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(stdlib) proc_lib.erl:797: :proc_lib.stop/3

Elixir/Phoenix can't connect to cloud hosted Postgres DB

trying to get my first Phoenix app connected to Compose's Cloud Hosted Postgres DB and am unable to connect. I have verified that it is up and running. I get the following error:
[error] GenServer #PID<0.178.0> terminating
** (DBConnection.ConnectionError) tcp connect (postgres://*****#aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain - :nx
domain
(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
Last message: nil
State: Postgrex.Protocol
** (Mix) The database for HelloPhoenix.Repo couldn't be created: an exception was raised:
** (DBConnection.ConnectionError) tcp connect (postgres://****#aws-us-east-1-portal.5.dblayer.com:16786/compose:5432): non-existing domain -
:nxdomain
(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
These are my dependencies:
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}]
end
And my DB Config:
# Configure your database
config :hello_phoenix, HelloPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
username: "admin",
password: "******",
database: "compose",
hostname: "postgres://admin:******#aws-us-east-1-portal.5.dblayer.com:16786/compose",
pool_size: 10
And using this version of PostgresSQL : PostgreSQL 9.4.10
Any help would be greatly appreciated. :-) Y'all take care! -Chris
The hostname of your database is aws-us-east-1-portal.5.dblayer.com, not the whole postgres://.... Also, you need to specify the port since it's not the default PostgreSQL port. This should work:
config :hello_phoenix, HelloPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
username: "admin",
password: "******",
database: "compose",
hostname: "aws-us-east-1-portal.5.dblayer.com",
port: 16786,
pool_size: 10
Since it seems you have a full database URL, probably the easiest solution for you will be to use that URL and let ecto decode it into the constituent parts that it needs for connecting.
config :hello_phoenix, HelloPhoenix.Repo,
adapter: Ecto.Adapters.Postgres,
url: "postgres://admin:******#aws-us-east-1-portal.5.dblayer.com:16786/compose",
pool_size: 10
Here's the documentation for that feature: https://hexdocs.pm/ecto/Ecto.Repo.html#module-urls