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

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.

Related

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/

Cant Access PostgreSQL on Google Cloud SQL from NestJS project on Google App Engine

This is my first question in Stack Overflow so please excuse me when my information is lack.
Issue
I am struggling to connect PostgreSQL on CloudSQL from NestJS on Google App Engine.
When I try to use the application in local environment the program works but when it comes to production in Google App Engine then it does not work.
Since i struggled days, I decided to ask awesome community here.
My Environment
Node.js: v10.19.0
NestJS: 6.10.5
TypeORM
PostgreSQL: 11.5.1
My app.yaml
runtime: nodejs10
env: standard
default_expiration: "4d 5h"
env_variables:
DATABASE_HOST: < public IP for Cloud SQL instance >
DATABASE_USERNAME: username
DATABASE_PASSWORD: password
DATABASE_NAME: databasename
INSTANCE_CONNECTION_NAME: "PROJECT_ID:REGION:INSTANCE_ID:DATABASE_NAME"
handlers:
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Error
[Nest] 18 - 02/27/2020, 8:25:46 AM [TypeOrmModule] Unable to connect to the database. Retrying (3)... +34816ms
2020-02-27 08:25:46 default[20200227t163916] Error: connect ETIMEDOUT 34.84.188.209:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:
Others Operations
GAE Service Account
Also I added the Cloud SQL Client authority my GAE Service account (something like this service-PROJECT_ID#gae-api-prod.google.com.iam.gserviceaccount.com).
I also added package.json as written in below:
"engines": {
"node": "10.x.x"
},
In the typeorm options, I added extra socketpath.
extra: {
socketPath: `/cloudsql/<INSTANCE_CONNECTION_NAME>/`,
},
I do not understand if this option should be set or not (I have tried both).
socketPath: `/cloudsql/<INSTANCE_CONNECTION_NAME>/.s.PGSQL.5432
or
socketPath: `/cloudsql/<INSTANCE_CONNECTION_NAME>
According to the example provided in GitHub
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/cloudsql_postgresql/app.flexible.yaml
The INSTANCE_CONNECTION_NAME environment variable does not include the DATABASE_NAME as a parameter.
e.g. my-awesome-project:us-central1:my-cloud-sql-instance
Maybe this could be causing that the name of the instance is not being resolved for the Proxy.

SailsJS deployment to Heroku, connect to Mongolabs MongoDB

I am right now attempting my first Heroku deployment of a SailsJS API. My app uses SailsJS v0.11 andsails-mongo 0.11.2.
I have updated config/connections.js to include the connection information to MongoDB database I have hosted for free at Mongolab.
mongodb: {
adapter: 'sails-mongo',
url: "mongodb://db-user:password123#ds047812.mongolab.com:47812/testing-db"
}
Also updated config/models.js to point to that adapter.
module.exports.models = {
connection: 'mongodb',
migrate: 'safe'
};
This is basically all I have changed from running the code locally, when I deploy to Heroku the app crashes and I get this error...
/home/zacharyhustles/smallChangeAPI/node_modules/connect-mongo/lib/connect-mongo.js:186
throw err;
^
at Socket.emit (events.js:107:17)
2015-07-08T19:37:00.778316+00:00 app[web.1]:
at Socket.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
Error: Error connecting to database: failed to connect to [localhost:27017]
How do I get rid of this, and make sure Sails does not try connecting to localhost db?
Ok, the problem was with storing sessions.
My solution was to setup a Redis database to store sessions.
In config/sessions.js make sure everything is commented out except for the method you want for session store.
Mine looked like this:
adapter: 'redis',
host: 'example.redistogo.com',
port: 1111,
db: '/redistogo',
pass: 'XXXXXYYYYYYXYXYXYYX',
This solved my posted problem, hope this helps another person out.

Alembic / Flask-migrate migration on Heroku runs but does not create tables

I am attempting to deploy a Flask app to Heroku. I have pushed to Heroku and can access my login page but any call to the db gives an OperationalError:
2014-01-29T12:12:31.801772+00:00 app[web.1]: OperationalError: (OperationalError) no such table: projects_project u'SELECT
Using Flask-migrate I can successfully run local migrations and upgrades:
INFO [alembic.migration] Context impl PostgresqlImpl.
INFO [alembic.migration] Will assume transactional DDL.
INFO [alembic.migration] Running upgrade None -> 4b56d58e1d4c, empty message
When I try to upgrade on Heroku using heroku run python manage.py db upgrade the upgrade appears to happen, but the Context impl. is now SQLite?:
(dev01)Toms-MacBook-Pro:dev01 kt$ heroku run python manage.py db upgrade
Running `python manage.py db upgrade` attached to terminal... up, run.9069
INFO [alembic.migration] Context impl SQLiteImpl.
INFO [alembic.migration] Will assume non-transactional DDL.
INFO [alembic.migration] Running upgrade None -> 4b56d58e1d4c, empty message
Running Heroku pg:info gives:
=== HEROKU_POSTGRESQL_PINK_URL (DATABASE_URL)
Plan: Dev
Status: available
Connections: 0
PG Version: 9.3.2
Created: 2014-01-27 18:55 UTC
Data Size: 6.4 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
The relevant logs for the Heroku upgrade are:
2014-01-29T12:55:40.112436+00:00 heroku[api]: Starting process with command `python manage.py db upgrade` by kt#gmail.com
2014-01-29T12:55:44.638957+00:00 heroku[run.9069]: Awaiting client
2014-01-29T12:55:44.667692+00:00 heroku[run.9069]: Starting process with command `python manage.py db upgrade`
2014-01-29T12:55:44.836337+00:00 heroku[run.9069]: State changed from starting to up
2014-01-29T12:55:46.643857+00:00 heroku[run.9069]: Process exited with status 0
2014-01-29T12:55:46.656134+00:00 heroku[run.9069]: State changed from up to complete
Also, heroku config gives me:
(dev01)Toms-MacBook-Pro:dev01 kt$ heroku config
=== myapp Config Vars
DATABASE_URL: postgres://xxx.compute-1.amazonaws.com:5432/da0jtkatk6057v
HEROKU_POSTGRESQL_PINK_URL: postgres://xxx.compute-1.amazonaws.com:5432/da0jtkatk6057v
where [xxx == xxx]
How is the Context impl. set? Apart from this obvious difference between working local and heroku, I can't work out what's happening or how I should debug. Thanks.
The URL for the database is taken from the SQLALCHEMY_DATABASE_URI configuration in your Flask app instance. This happens in the env.py configuration for Alembic that was created in the migrations folder.
Are you storing the value of os.environ['DATABASE_URL'] in the configuration before you hand over control to Flask-Migrate and Alembic? It seems you have a default SQLite based database that never gets overwritten with the real one provided by Heroku.

PropelBundle database:create for postgres

I've installed propel bundle for symfony2.
my database configuration is:
propel:
dbal:
driver: pgsql
user: postgres
password: postgres
dsn: pgsql:host=localhost;port=5432;dbname=test_database
options: {}
attributes: {}
When i wan to create this database from console (console propel: database:create) i have got strange error : Unable to open PDO connection [wrapped: SQLSTATE[08006] [7] FATAL: database "pgsql" does not exist.
i created pgsql database on my localhost and everything was good. Database "test_database" was succesfull created. Can somebody explain me why i got this previous error? On mysql i've created database without any problems.
This issue was a bug in the PropelBundle, it has been fixed by the following commit (even if the commit message is about MySQL, it fixes other RDBMS): https://github.com/propelorm/PropelBundle/commit/b4475d27fb1eb846d10cc2d2e2bd164f037508e3
I 've installed new PropelBundle via Composer and everything is ok now. I think maybe it was a problem with 1.0 bundle , now I have 1.1