Skip to content

Instantly share code, notes, and snippets.

@Yonkokilasi
Last active May 14, 2017 17:49
Show Gist options
  • Save Yonkokilasi/5d11ab8931e51e22eb1797be1c58daa2 to your computer and use it in GitHub Desktop.
Save Yonkokilasi/5d11ab8931e51e22eb1797be1c58daa2 to your computer and use it in GitHub Desktop.
Pushing a psql application to Heroku
1. In App.java
place this code in the public class App section
static int getHerokuAssignedPort() {
ProcessBuilder processBuilder = new ProcessBuilder();
if (processBuilder.environment().get("PORT") != null) {
return Integer.parseInt(processBuilder.environment().get("PORT"));
}
return 4567; //return default port if heroku-port isn't set (i.e. on localhost)
}
public static void main(String[] args) {
port(getHerokuAssignedPort());
staticFileLocation("/public");
String layout = "templates/layout.vtl";
From there login to heroku and do the following commands in the project directory
2. $heroku login
3. $heroku create
4. $ heroku addons:create heroku-postgresql:hobby-dev --app name-of-app e.g $ heroku addons:create heroku-postgresql:hobby-dev --app gentle-shelf-93411
This will be seen
Creating heroku-postgresql:hobby-dev on ⬢ gentle-shelf-93411... free
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-curly-98998 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation
5. $ heroku psql postgresql-curly-98998 (Database URL given above)
This will be seen
--> Connecting to postgresql-curly-98998
psql (9.3.16, server 9.6.1)
WARNING: psql major version 9.3, server major version 9.6.
Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
gentle-shelf-93411::DATABASE=>
6. Exit the psql and run $ heroku config - this will give you necessary details to be used below.
This will be seen
DATABASE_URL: postgres://xnurmsxroim:06d340be81b008bbcbe2b8d450c210099ee721c87af6856c06d4f35985697@ec2-23-21-235-134.compute-1.amazonaws.com:5432/d8a4trqqn4cdcg
xnurmsxroim - USERNAME
06d340be81b008bbcbe2b8d450c210099ee721c87af6856c06d4f35985697 - PASSWORD
ec2-23-21-235-134.compute-1.amazonaws.com:5432/d8a4trqqn4cdcg - PATH
Your DB.java looks like this
public class DB {
public static Sql2o sql2o = new Sql2o("jdbc:postgresql://localhost:5432/app_name", username, password);
}
This is to be used for local development
7. when pushing to heroku this is what should be present
public class DB {
public static Sql2o sql2o = new Sql2o("jdbc:postgresql://PATH","USERNAME", "PASSWORD");
}
See step 6
Now pushing the database
Place the code in your terminal
8. Let's say our local database name is wildlife_tracker then
local_database_name = wildlife_tracker
your-app-name is seen above in step 5. Remember postgresql-curly-98998
your-app-name = postgresql-curly-98998
$ heroku pg:push local_database_name your-app-name.
That's it. You are now live.
If you have a question please leave a comment below
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment