0

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.

7
  • Are you really using rails new so often that this matters? Commented Mar 19, 2014 at 20:49
  • I am using it with a template to automatically add gems and run generators. Some generators will require rake db:create to already have run as they create migrations. It wont create the db's if the username is incorrect thus losing the usefulness of the template (works great with sqlite) Commented Mar 19, 2014 at 20:56
  • 1
    I've never heard of such a thing, myself, and Meagar has a point that the effort is quite minimal. However I just stumbled upon this which might possibly help you do what you're looking to do. Edit: Nevermind I guess you're already using templates. Never used them myself, so can't advise beyond that. I assume there's no easy way to affect the generated database.yml Commented Mar 19, 2014 at 20:56
  • Thanks Paul, this line in that post will do the trick run "cp config/database.yml config/database.example" Commented Mar 19, 2014 at 20:59
  • Ok not quite, but a step in the right direction. Will change it to copy template in from outside the app but would still need to play around to change the db names once it is copied. Commented Mar 19, 2014 at 21:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

And adminpack was necessary because ...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.