0

Heroku provides its own database name and other credentials, but my local database name is different.How can I change the database name according to the database credentials provided by heroku during production?

2 Answers2

0

Use a package like dotenv. dotenv and variants of it likely exist for whatever language you're using.

Basically, you want to use environment variables instead of hard coding values into your code. So, instead of writing something like this:

my_database_connect('my_username', 'abc123')

You'd write:

my_database_connect(process.env.DB_USERNAME, process.env.DB_PASSWORD)

Heroku will already have these environment variables set on the "config" tab of your app. Then for local development, you'll create a file called .env and have this text in it:

DB_USERNAME=my_username
DB_PASSWORD=abc123

Don't commit .env to your git repository – it should only live on your machine where you develop. Now your code will run locally as well as on Heroku, and connect to the proper database depending on the environment it's running in.

Here's an article that explains this more thoroughly for node.js, although this is basically the best practice for general development: https://medium.com/@rafaelvidaurre/managing-environment-variables-in-node-js-2cb45a55195f

Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76
0

First I created an application name on Heroku. Then I deployed my app to heroku by connecting to github.

Heroku provides the database credentials after we deploy our applications. Then I redeployed the app through github by changing the configuration in application.properties file as follows:

#localhost configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost/transactions?useSSL=false
SPRING_DATASOURCE_USER=postgres
SPRING_DATASOURCE_PASSWORD=some_pass

#server database configuration
SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_URL=jdbc:postgresql://ec2-23-23-247-222.compute-1.amazonaws.com/d6kk9c4s7onnu?useSSL=false
SPRING_DATASOURCE_USER=rimjvlxrdswwou    
SPRING_DATASOURCE_PASSWORD=dd903753bc0adffb96ce541b1d55fb043472e32e28031ddc334175066aa42f69

Then you have to edit the config vars according to your application.properties files as shown in the figure below

config_var.png