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

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.

Related

Database transfer from Heroku to Digital Ocean

I'm currently in the process of switching my cloud server from Heroku to Digital Ocean. However is there a way to migrate the database from the heroku server to the digital ocean one? I use postgresql for my database
I hope you already got a solution, but in case you didn’t, I’ll provide a simple guide on how I did it. I am going to assume that you have already created a postgres database on digitalocean. Also you need navigate to your project directory and log in to heroku using the heroku cli. And, you need to have postgresql installed or a psql client. Installing postgresql would do it as it comes with psql.
Step 1: Create a backup and download the backup from heroku postgres
heroku pg:backups:capture --app <app_name>
heroku pg:backups:download --app <app_name>
The first command will create a backup of your database and the second command will download it to your current directory, its a .dump file. If you would like to read more, here is an article.
Step 2: Connect to your remote (digital ocean’s) database using psql
Before you can do this, you need to go and add your machine you are connecting from to the list of database’s list of trusted sources, If you don’t, you’ll get a Connection Timed Out error. That’s because the database’s firewall doesn’t allow you to connect to the database from your local machine or resource (for security reasons).
Step 3: Import the Database
pg_restore -d "postgresql://<database_username>:<database_password>#<host>:<port>/<database>?sslmode=require" --jobs 4 -c "/path/to/dump_file.dump"
This will import your database from your dump file. Just substitute the variables will your connection parameters that you get from your dashboard. If you would like to read more, here is another article for this step.
Another thing to make clear is, sometimes, you will see some harmless error messages when running this command, but it will push through anyway. To learn more about pg_restore read this article.
And that’s it, your database has been migrated. Now, can you confirm it worked?, well, as for me, I used pgAdmin to connect to the remote database and I saw the tables and data as expected.
Hope this helps anyone with the same problem :)

Run local database with Heroku

What I need to know if I use service to create PostgreSQL in Heroku, can I set up this database locally in development mode?
I couldn't find in their documentation on how to do this.

Heroku Permanent Database Credentials

I've decided to save time on the ops side of things and move to Heroku. I'm planning to have a production dyno on Heroku with a postgres database AND another dyno that reads from the same database.
However when I opened the settings of postgres, it said:
Database Credentials
Get credentials for manual connections to this database.
Please note that these credentials are not permanent.
Heroku rotates credentials periodically and updates applications where this database is attached.
What's a good way to go about this?
From Heroku Documentation,
Credentials
Do not copy and paste database credentials to a separate environment or into your application’s code. The database URL is managed by Heroku and will change under some circumstances such as:
User initiated database credential rotations using heroku pg:credentials:rotate.
Catastrophic hardware failure leading to Heroku Postgres staff recovering your database on new hardware.
Automated failover events on HA enabled plans.
It is best practice to 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-name) your_process
This way, you ensure your process or application always has correct database credentials.
May be attaching the same database to two heroku-apps will better suit you. In this way, pg creds will be auto-managed by heroku.
I am also using this technique. I have one client-facing app and another operation-app sharing the same database instance.
You can either do this using UI or via CLI
see Share database between 2 apps in Heroku

How do I get started if I want to use PostgreSQL for local use?

Good day,
Currently I use MS Access at home for several Databases (for personal use).
At work, I use PostgreSQL, which is infinity times better. I want to start using postgres for my personally used databases, but I don't know where to start.
I've tried reading the documentation, but still don't know how to start. I don't have a server at home; is it possible I can just make a local database/tablespace? Or would I have to host a virtual server?
Note that I am willing to use other open source databases if there is an easy option out there - MS access is just so... terrible.
Thanks,
So, it seems you have Windows at home. You just need to download full installer for PostgreSQL:
http://www.postgresql.org/download/windows/
After installation it will automatically add starting postgres server as a service on local machine. That means, server will always run in background, but you can disable that later, or just uninstall.
After that, you can use pgAdmin (included in default installation package) or other client tools to access the DB engine.
UPD in pgadmin, create connection with this settings:
'localhost' as hostname;
port - 5432;
user, database - postgres (for testing purpose only - you should create your own user and tables with restricted rights later).
Password for postgres (that is DB admin user) must be entered during installation process.
Server settings are stored somewhere here:
"C:\Program Files\PostgreSQL\9.3\data"
pg_hba.conf - Client Authentication Configuration File
postgresql.conf - Configuration File

Play Framework + Heroku + Postgres not able to connect

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.