Play Framework + Heroku + Postgres not able to connect - postgresql

I've been having a heck of a time getting my Play! framework java app to run on Heroku, and I think I've narrowed it down to the Postgres JDBC driver not liking Heroku's DATABASE_URL parameter because it starts with postgres: and not postgresql:.
What is the proper way to configure a play! 2.0 app to connect to a heroku-provided Postgres instance?
I've tried variations on the following:
PLAY_OPTS="-Ddb.default.url=$DATABASE_URL -Ddb.default.driver=org.postgresql.Driver"
But upon startup I get a SQLException that no suitable driver can be found for $DATABASE_URL.

No need to pass them in as system properties you can pickup Heroku environment variables in your application.conf file
...
db.default.driver=org.postgresql.Driver
db.default.url=${DATABASE_URL}
Then define this in your Procfile
web: target/start -Dhttp.port=${PORT} ${JAVA_OPTS} -Dconfig.resource=application.conf
It should pick up the DATABASE_URL property for the Heroku environment. Although, I recommend creating a configuration file that is specific for the Heroku environment (i.e. heroku-prod.conf), but this is just an example.

Related

How can I access to a PostgreSQL DB from outside of a heroku app (for a Python app)

I have a PostgreSQL DB hosted in heroku and I want to get access from other applications which aren't hosted in heroku, but I saw in the DB settings that the credentials are not permanent.
How can I get access from other application always having updated credentials?
Heroku recommends using the Heroku CLI to fetch fresh credentials every time you run your external application:
Always fetch the database URL config var from the corresponding Heroku app when your application starts. For example, you may follow 12Factor application configuration principles by using the Heroku CLI and invoke your process like so:
DATABASE_URL=$(heroku config:get DATABASE_URL -a your-app) your_process
This way, you ensure your process or application always has correct database credentials.
In this example, your_process will see an environment variable called DATABASE_URL that is set to the same value as the DATABASE_URL config far on the Heroku app called your-app.
Since you are using Python, here is one way to access that value:
import os
database_url = os.getenv("DATABASE_URL", default="some_default_for_local_development")

How to connect Eclipse ditto to mongodb cloud

I am fairly new to Eclipse Ditto and have just started using it for my project.
I am trying to connect Cloud hosted mongodb instance to ditto.
Following the documentation I know that I need to add some variables and pass them to docker-compose. The problem is that I do not know what should be the values of these variables as there are no examples.
Are all these variables necessary or will just the URI work?
This is my current .env file config
MONGO_DB_URI=mongodb+srv://username:pass#IP
MONGO_DB_READ_PREFERENCE=primary
MONGO_DB_WRITE_CONCERN=majority
The command I am using to start ditto is
docker-compose --env-file .env up
I have removed mongodb service from docker-compose.yml
Nice to hear that you started using Ditto in your project.
You need to set the following env variables to connect to your Cloud hosted MongoDB.
MONGO_DB_URI: Connection string to MongoDB
For more detailes see: https://docs.mongodb.com/manual/reference/connection-string/
If you have a ReplicaSet your MongoDB URI should look like this: mongodb://[username:password#]mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
I assume you also need to enable SSL to connect to your MongoDB.
To do so set this env var.
MONGO_DB_SSL_ENABLED: true
If you want to use a specific Ditto version you can set the following env var
DITTO_VERSION= e.g. 2.1.0-M3
If you use .env as file name you can start Ditto with:
docker-compose up
The other options for pool size, read preference and write concern aren't necessary as there are default values in place.

How to deploy a FastAPI app using PostgreSQL as a database on Heroku

I developed a FastAPI app in a Virtual Environment using an SQLite database but deployed it on Heroku with a PostgresSQL database on Heroku as suggested in the tutorial. Although it worked on my PC, adding PostegresSQL as an addon & replacing the value of SQLALCHEMY_DATABASE_URL in the database.py broke everything. Note that I've properly frozen the dependencies on the requirements.txt file. Yet I can't figure out what went wrong.
For further clarification, I've pushed my code to GitHub & it can be accessed at this repository - Self_calculation.
If you are using Postgres Addon on Heroku, probably your solution is simple.
Use os.environ to get the connection parameters , don't try to connect directly, it's Heroku's recommended solution from Heroku Postgres
import os
DATABASE_URL = os.environ.get('DATABASE_URL')
I just need to run this:
1)heroku git:remote -a my_heroku_app_name
2)heroku logs --tail
after that I could see my problem.
In my case I forgot to change postgres url in alembic.ini file

Make Flask use Postgres on Heroku

i have been following the Flask book by Miguel Ginberg and I am thinking about how to deploy my app and use the PostgresDB.
In my local production config I have to manually go in and run Role.insert_roles() before any roles can be assigned.
How do I do this in Heroku with postgres? In fact, how do you connect to the postgres db? It is not really clear where in the code postgres takes over using the environment variable:
https://github.com/miguelgrinberg/flasky/blob/master/config.py
I have a feeling my app is just running sqlite and the book isn't really clear on how to switch over.
SOLUTION:
if you have deployed to heroku and you have not changed the environment variables:
DATABASE_URI to SQLALCHEMY_DATABASE_URI
FLASK_CONFIG = heroku
FLASKY_ADMIN = your email
then ran in your shell:
heroku run python manage.py shell
db.create_all()
db.commit()
Role.insert_roles()
then you are probably running the development config from the SQLite database!
If you want to connect manually, you could use the http://initd.org/psycopg/ libarary directly. Flask-SQLAlchemy provides uses psycopg underneath the hood itself, see here - in your example it may be easier to continue using SQLAlchemy. More information here.
I had the same problem and solved like the follow:
1. provisioning a db
$:heroku addons:create heroku-postgresql bobby-dev
......
add database_url:
$:hero config -s |grep HEROKU-PSOTGRESQL
( then show HEROKU_POSTGRESQL_RED_URL=..... )
$:heroku pg:promote HEROKU-POSTGRESQL-RED
install this extention:psycopg2
3.change the config.py:
config = {
.....
'default' : ProductionConfig
}

Unicorn Procfile and development databases (Ruby on Rails 4, PostgreSQL, Heroku, Resque)

I am developing a RoR4 app using a Postgre database that does some background database processing with resque and is hosted on Heroku. My question regards running locally for development.
It is my understanding (in this particular project) that when I start the web server using rails server, it connects to a development database, and when I start it using foreman start (with an appropriate Procfile), it connects to some other local database.
My problem is that my Resque jobs look for ActiveRecords in the development database, forcing me to use rails server. However, I need to access some environment variables, stored in a .env file, and it is my understanding that only foreman is able to read these environment variables.
How do I approach a workaround for this problem?
More specifically, how can I get my Resque jobs to look for ActiveRecords in the same database that foreman start uses? Or, how do I have foreman start use the the development database?
My guess for the latter would be edit the Procfile, but I haven't had luck finding a simple solution.
Procfile.dev:
web: bundle exec unicorn -c ./config/unicorn.rb -E $RAILS_DEV
NOTE: $RAILS_DEV=development
Any help would be appreciated. Thanks.
I found out what the problem is. It turns out that my .env file had an explicit URL to connect to my Heroku database. Once I removed that line, foreman start connected to my development database.