Vapor Framework : Configure a postgres connection with SSL - postgresql

I'm trying to connect to my Heroku PostgreSQL database but I have the following error :
cannotEstablishConnection("FATAL: no pg_hba.conf entry for host \"37.167.93.189\", user \"clpnkpyttmdtyq\", database \"d3h6147v73mgtu\", SSL off\n")
I know that Heroku postgres databases need to use an SSL connection but I don't know how to configure the connection on my Droplet object.
This is my postgresql.json configuration file :
{
"host": "ec2-54-163-224-108.compute-1.amazonaws.com",
"user": "clpnkpyttmdtyq",
"password": "99201aa07c48e18e7bdf210937857b85bee37cd8d8cb904381b1ddff934c7a4f",
"database": "d3h6147v73mgtu",
"port": 5432
}
Maybe there is ssl parameter that I don't know ?
How I add the VaporPostgresSQLProvider :
let drop = Droplet()
// Tell the droplet to use our SQL provider service
try drop.addProvider(VaporPostgreSQL.Provider.self)
Any ideas ?
When I try with my local postgres database, it works, because it don't need ssl connection.

For Heroku we need unverifiedTLS transport.
https://api.vapor.codes/postgresql/latest/PostgreSQL/Classes/PostgreSQLConnection/TransportConfig.html
let pgURL = Environment.get("DATABASE_URL") ?? "postgres://user:password#host:port/database"
let pgConfig = PostgreSQLDatabaseConfig(url: pgURL, transport: PostgreSQLConnection.TransportConfig.unverifiedTLS)!

it's a process that personally cost me a lot, this solution works for me, try this
On file Config > secrets > postgresql.json add this configuration (for use on local or remote, if this file not exist, create this)
{
"host": "127.0.0.1",
"user": "your_user_pc",
"password": "",
"database": "your_user_pc",
"port": 5432
}
The user can get it from Terminal
$ cd ~
On your file Procfile (sited on your project, show via finder) edit and add this code
web: App --env=production --workdir="./"
web: App --env=production --workdir=./ --config:servers.default.port=$PORT --config:postgresql.url=$DATABASE_URL
Now you can re-launch your application to heroku, you should consider having the server configured correctly from heroku with all its credentials and add-ons of Postgresql from the interface of Heroku
Note: And do not forget every change you make, run "vapor build" or "vapor build --clean"

Vapor 4 + build stack heroku-20 + Heroku Postgres's standard plan
Rijel David's suggestion did the trick for me
But unverifiedTLS syntax has slightly changed
if let databaseURL = Environment.get("DATABASE_URL"), var postgresConfig = PostgresConfiguration(url: databaseURL) {
postgresConfig.tlsConfiguration = .forClient(certificateVerification: .none)
app.databases.use(.postgres(
configuration: postgresConfig
), as: .psql)
} else {
// ...
}
Check out Vapor doc - https://docs.vapor.codes/4.0/deploy/heroku/

Related

Quarkus: Datasource '<default>': FATAL: no pg_hba.conf entry for host

I wrote a Quarkus JPA/Hibernate app with PostgreSQL DB perfectly working with a JVM - run by 'quarkusDev' gradle task.
When I compile the native application with
#!/usr/bin/env bash
export QUARKUS_PROFILE=****
export PG_USERNAME=*****
export PG_PASSWORD=*****
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
./gradlew build -Dquarkus.package.type=native
the compilation goes fine, but when I run the native image I get:
14:41:23 WARN [io.ag.pool] Datasource '<default>': FATAL: no pg_hba.conf entry for host "***.***.***.***", user "xxxxxxxxxxxxxx", database "*****", SSL on
14:41:23 WARN [or.hi.en.jd.sp.SqlExceptionHelper] SQL Error: 0, SQLState: 28000
14:41:23 ERROR [or.hi.en.jd.sp.SqlExceptionHelper] FATAL: no pg_hba.conf entry for host "***.***.***.***", user "xxxxxxxxxxxxxx", database "*****", SSL on
14:41:23 ERROR [io.qu.application] Failed to start application: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
I tried to add a pg_hba.conf file:
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
and put it to
/src/main/resources
and/or to the native image directory, but without luck.
Also, I configured the resources for my native image adding a 'resources-config.json' to '/src/main/resources':
{
"resources": [
{
"pattern": ".*\\.yaml$"
},
{
"pattern": ".*\\.conf$"
},
{
"pattern": ".*\\.txt$"
},
{
"pattern": ".*\\.html$"
}
]
}
I added the following line to 'application.yml'
quarkus:
native:
additional-build-args: -H:ResourceConfigurationFiles=resources-config.json
But even doing so, the pga_hba.conf is not found, and when run, the native image keeps on complaining.
The issue is with the pg_hba.conf being used by the Postgres server. You need to find it and add an entry that will match the connection parameters you are sending to the server.
It turned out that I missed to pass environment variables to the executable.
Of course - obvious after realizing what the problem was! - you not only need to pass DB Environment Variables when you compile the native image - so the tests can be run - but you also need to pass them when you launch it.
So, a script like this solves the issue:
#!/usr/bin/env bash
export PG_USERNAME=*****
export PG_PASSWORD=*****
./build/my-native-image.runner

I can not connect to Postgres DB with Strapi on Heroku

Trying to deploy Strapi on Heroku with Postgres as described here
https://strapi.io/documentation/v3.x/deployment/heroku.html
But I get this error
error: no pg_hba.conf entry for host "84.212.51.43", user "ssqqeaz***", database "d6gtu***", SSL off
I use Heroku Postgres add-on.
My database config:
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 27017),
database: env('DATABASE_NAME', 'strapi'),
username: env('DATABASE_USERNAME', ''),
password: env('DATABASE_PASSWORD', ''),
},
options: {
ssl: true
},
},
},
});
Why? Please help!
try to change ssl : true into ssl : false
The current configuration you've posted will not work with a Heroku Postgres database. The primary concern here is that you're reading components of your postgres database url out of manually set config vars. This is very much recommended against by Heroku because they may need to move the database to a new host in the case of disasters. DATABASE_URL is set by Heroku when you create a database on an app and it's the one config var you can rely on to stay up-to-date. Moving on...
You will need to parse the username, password, host, port and database name out of the DATABASE_URL config var and supply those to the attributes of the settings block. Based on the error you provided, I can tell you're not presently doing this because Heroku databse usernames all start with a 'u', so something is very wrong if you get the error user "ssqqeaz***". As a first step you might try hard coding these values in the settings block to make sure it works (make sure to rotate the credentials after you do it, or otherwise clean up your git history to prevent leaked creds). The pattern for a postgres connection url is something like this: postgres:// $USERNAME : $PASSWORD # $HOSTNAME : $PORT / $DATABASE_NAME.
Not sure if it will help moving your config around...
remove ssl from option Key
insert ssl after password inside of settings Key
eg.
ssl: env.bool('DATABASE_SSL', false),
also check your app config vars inside of Heroku and make sure you have the required postgres config vars setup and they match the heroku generated DATABASE_URL config var.
lastly check your ./config/server.js file and make sure your host is 0.0.0.0
eg.
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET', '**********************************'),
},
},
});

Setting up Strapi using MongoDB via the PM2 Runtime

I'm quite new to Strapi and I'm following the Strapi deployment documentation at https://strapi.io/documentation/3.0.0-beta.x/guides/deployment.html#configuration. I have setup strapi using mongodb and it seems to work both in production and dev on my server. I can create content types and add data...
Now I'm trying to start Strapi using the PM2 Runtime. I have setup the ecosystem.config.js file (see below) and I run pm2 start ecosystem.config.js. The Strapi app seems to start just fine, but now what happens in the browser is that I am prompted with a new admin user. Seems like all users and data is lost... Mongo db not accessed or whats going on?
this is my ecosystem.config.js file
module.exports = {
apps : [{
name: 'cms.strapi',
cwd: '/var/www/domain/public_html',
script: 'server.js',
env: {
NODE_ENV: 'production',
DATABASE_HOST: '127.0.0.1',
DATABASE_PORT: '28015',
DATABASE_NAME: 'db-name',
DATABASE_USERNAME: 'db-u-name',
DATABASE_PASSWORD: 'pw',
},
}],
};
What am I missing?
Hi Jim and thanks for your reply! I believe the problem was a mixup between the prod and the dev environment. Sorry, my bad. I thought I was in one environment when I was really in the other. I guess it should be obvious when you start the server from the prompt whether your starting dev or prod, but once the web server is up and running in the browser I guess you can't tell from the gui whether it's the one or the other. At least I can't find one other than that the admin usernames (and possibly data) are different... Hmm..
Anyway my production/database.json file looks like this:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"uri": "mongodb://localhost:27017/db-prod",
"database": "db-prod",
"host": "127.0.0.1",
"srv": false,
"port": 27017,
"username": "u-name-prd",
"password": "pw"
},
"options": {
"ssl": false
}
}
}
}
PM2 Runtime seems to be working correctly with Strapi and Mongo now :-)

what is client property of knexfile.js

In knex documentation of configuration of knexfile.js for PostgreSQL, they have a property called client, which looks this way:
...
client: 'pg'
...
However, going through some other projects that utilize PostgreSQL I noticed that they have a different value there, which looks this way:
...
client: 'postgresql'
...
Does this string correspond to the name of some sort of command line tool that is being used with the project or I misunderstand something?
Postgresql is based on a server-client model as described in 'Architectural Fundamentals'
psql is the standard cli client of postgres as mentioned here in the docs.
A client may as well be a GUI such as pg-admin, or a node-package such as 'pg' - here's a list.
The client parameter is required and determines which client adapter will be used with the library.
You should also read the docs of 'Server Setup and Operation'
To initialize the library you can do the following (in this case on localhost):
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
})
The standard user of the client deamon ist 'postgres' - which you can use of course, but its highly advisable to create a new user as stated in the docs and/or apply a password to the standard user 'postgres'.
On Debian stretch i.E.:
# su - postgres
$ psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'SecretPasswordHere';"
Make sure you delete the command line history so nobody can read out your pwd:
rm ~/.psql_history
Now you can add a new user (i.E. foobar) on the system and for postgres
# adduser foobar
and
# su - postgres
$ createuser --pwprompt --interactive foobar
Lets look at the following setup:
module.exports = {
development: {
client: 'xyz',
connection: { user: 'foobar', database: 'my_app' }
},
production: { client: 'abc', connection: process.env.DATABASE_URL }
};
This basically tells us the following:
In dev - use the client xyz to connect to postgresqls database my_app with the user foobar (in this case without pwd)
In prod - retrieve the globalenv the url of the db-server is set to and connect via the client abc
Here's an example how node's pg-client package opens a connection pool:
const pool = new Pool({
user: 'foobar',
host: 'someUrl',
database: 'someDataBaseName',
password: 'somePWD',
port: 5432,
})
If you could clarify or elaborate your setup or what you like to achieve a little more i could give you some more detailed info - but i hope that helped anyways..

Remote MongoDB access through Cloud 9 gives login failed exception

I'm using the Cloud 9 IDE to develop an application using MongoDB. I created a database called "appdata" at MongoLab and the following user:
{
"_id": "appdata.admin",
"user": "admin",
"db": "appdata",
"credentials": {
"SCRAM-SHA-1": {
"iterationCount": 10000,
"salt": "K/WUzUDbi3Ip4Vy59gNV7g==",
"storedKey": "9ow35+PtcOOhfuhY7Dtk7KnfYsM=",
"serverKey": "YfsOlFx1uvmP+VaBundvmVGW+3k="
}
},
"roles": [
{
"role": "dbOwner",
"db": "appdata"
}
]
}
Whenever I try connecting to the database through Cloud 9 Shell using the following command (given by MongoLab with my newly created user):
mongo ds057244.mongolab.com:57244/appdata -u admin -p admin
I get the following error message:
MongoDB shell version: 2.6.11
connecting to: ds057244.mongolab.com:57244/appdata
2015-11-22T05:23:49.015+0000 Error: 18 { ok: 0.0, errmsg: "auth failed",
code: 18 } at src/mongo/shell/db.js:1292
exception: login failed
Also, on my javascript file running on Cloud 9, while following this tutorial (which uses mongoose to access the DB) I got stuck on the post route for bears. Whenever I send a post request through postman with the specified fields set, the route doesn't return anything, neither a bear created nor an error message, which makes me think the problem is also failing to login to the database. The previous get request is working just fine and my code is the exactly same as the tutorial.
Does anyone know what the problem in any of the cases and what I need to do to solve them?
The shell problem was fixed updating it to the Database version (which was 3.0.3).
For the javascript files, I restarted the tutorial and made sure I downloaded all necessary dependencies with the most recent stable version (not the ones shown on the tutorial), after that the problem was solved.