andreofthecape
andreofthecape

Reputation: 1196

Automatically set username in database.yml for Postgres

When you generate a new rails app with postgres as db it automatically set username to appname in database.yml.

You then have to manually change it to your username or the app will not run.

Is there any way to automatically set it to logged in user (at time of running rails new) or other preset username?

In development and test environments it will always be the same username and production does not matter as it always deployed on heroku.

I am on ubuntu, but would be great to hear solutions for mac users also.

Upvotes: 0

Views: 542

Answers (2)

andreofthecape
andreofthecape

Reputation: 1196

Ok I ended up doing this: In my template I added this line to replace the username: gsub_file "config/database.yml", /username: .*/, "username: myusername" I have a simple workflow of sole developer on dev machine so usernames for developement and test will always be the same and production does not matter as it deploys to heroku.

Upvotes: 1

anathema84
anathema84

Reputation: 1

It is a good habit to make a new user for each application. to do this, first create adminpack extension as follows:

$ psql postgres -c 'CREATE EXTENSION "adminpack”;'

Open database prompt to create a new user. lets say your app called blog:

$ psql -d postgres
postgres=# create role blog login createdb ;
postgres=# \q

Now create your database and associate that to the user you just made:

$ createdb -E UTF8 -w -O blog blog_development

To check of its created:

$ psql -l

Upvotes: 0

Related Questions