Creating a new database for second project on Prostgres - ruby-on-rails

I am starting a new project and want to use Postgres as my database. How can I implement a new database for this project with Postgres without causing problems with the other database I am using for another project?
Thank you!
EDIT
Not sure if it matters but these two projects have two different heroku accounts.

You tagged the question with the heroku tag. Heroku will take care of this for you.
Locally, you should first create a new user:
sudo -u postgres createuser -s your_user_name
For local development you can make this a super-user. Heroku will use sensible defaults in production.
Done that, configure your rails app and then create your db:
bin/rake db:create db:migrate

Related

Rails app works on localhost but no database on Heoku

First time posting a question.
I recently did a Rails project on Udemy and it works fine on localhost, when I pushed to Heroku I only see signup/login page, none of the 'New,Edit,Delete..' pages. Installed pg and rails_12factor in production, and sqlite in test and dev. The teacher hasn't answered on the forum in a week so looking in other places. Here's my GitHub link https://github.com/guruprasadnagarajan/taskr and this is the Heroic page https://taskr-guru.herokuapp.com
Thanks!
When you create a heroku app and after you've created the database, you need to also migrate and add data to the database. Make sure that you have valid users and other needed data. For example, you can access the rails console of your application by running:
heroku run rails c -a <your app's name>
Then you can create users from there using rails models like:
User.create(name: <a name>, email: <an email>)
Alternatively you may create the needed data in the db/seed.rb file and then just run the task:
heroku run rake db:seed -a <your app's name>
If you have done all these steps above and still have the same issue, I am not sure how to go around it.
Hope it helps, happy coding!

I would like to create new postgres sql DB for Heroku

I have deployed an application in Heroku. It is Rails applications. The DB is postgres. So this is the app in heroku. Now I want to create new app which will be a clone of the previous app. But it will have separate DB. How should I get going? I would also like to know about configuring the DB from heroku. I am not so techy so please go easy on me.
Thanks!
But it will have separate DB
So, just create a new heroku app, add PG add-on to it, and that's it. No more steps. It will have a separate database. I think you don't even have to add PG add-on, it probably is added by default.
If you wanted two apps use the same DB, then you'd have to do extra steps.
You can use Heroku Fork to clone your application. This will copy the app, environment variables and any postgres databases (and their data if you want it) to a separate Heroku application.
heroku fork --from old-app-name --to new-app-name

How do you access your heroku database (specifically postgres)?

When I am developing locally, I'll just run a
rails c
and I can fire of queries at the console.
However, how can I query and access my postgres database that is up on heroku?
You can run heroku run rails c --app <appname>. I will also add, if you want to view your database, you can make use of PG Commander https://eggerapps.at/pgcommander/ which actually automatically parses all the credentials out for you.
If you want to do this, run heroku config --app <appname> and then copy the DATABASE_URL. After you've done this, create a new favourite in your PG Commander and you'll see that it has already automatically filled in all the credentials based on what was in your clipboard.

Rails and PostgreSQL: Role postgres does not exist

I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.
For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error
FATAL: role "postgres" does not exist
I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.
I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.
Actually, for some unknown reason, I found the issue was actually because the postgresql role hadn't been created.
Try running:
createuser -s -r postgres
Note that roles are the way that PostgreSQL maintains database permissions. If there is no role for the postgres user, then it can't access anything. The createuser command is a thin wrapper around the commands CREATE USER, CREATE ROLE, etc.
Recently i got this problem immediately after installing postgres.
If it comes immediately after installation, you might be missing the default user, postgres.
In that case, you can create default user postgres using below command.
createuser -s -U $USER
Ex: createuser -s -U $USER
enter your required role name: postgres
enter password for your the user:
It will prompt you to enter required database role name and password
Once you complete the process, you can login to the postgres console using below command
psql -U 'your_database_name'
Ex: psql -U postgres
Here, You need to enter the password if you have given any, while creating the user.
Hope it helps :)
I was on OSX 10.8, and everything I tried would give me the FATAL: role "USER" does not exist. Like many people said here, run createuser -s USER, but that gave me the same error. This finally worked for me:
$ sudo su
# su postgres
# createuser -s --username=postgres MYUSERNAME
The createuser -s --username=postgres creates a superuser (-s flag) by connecting as postgres (--username=postgres flag).
I see that your question has been answered, but I want to add this answer in for people using OSX trying to install PostgreSQL 9.2.4.
This message pops up, when the database user does not exist. Compare the manual here.
Multiple local databases cannot be the explanation. Roles are valid cluster-wide. The manual again:
Note that roles are defined at the database cluster level, and so are
valid in all databases in the cluster.
You must be ending up in another database-cluster. That would be another server running on the same machine, listening to a different port. Or, more likely, on a different machine.
Could it be that the message comes, in fact, from the remote server?
I met this issue right on when I first install the Heroku's POSTGRES.app thing. After one morning trial and error i think this one line of code solved problem. As describe earlier, this is because postgresql does not have default role the first time it is set up. And we need to set that.
sovanlandy=# CREATE ROLE postgres LOGIN;
You must log in to your respective psql console to use this psql command.
Also noted that, if you already created the role 'postgre' but still get permission errors, you need to alter with command:
sovanlandy=# ALTER ROLE postgres LOGIN;
Hope it helps!
In the Heroku documentation; Getting started whit rails 4, they say:
You will also need to remove the username field in your database.yml
if there is one so: In file config/database.yml remove: username:
myapp
Then you just delete that line in "development:", if you don't pg tells to the database that works under role "myapp"
This line tells rails that the database myapp_development should be
run under a role of myapp. Since you likely don’t have this role in
your database we will remove it. With the line remove Rails will try
to access the database as user who is currently logged into the
computer.
Also remember to create the database for development:
$createdb myapp_development
Repleace "myapp" for your app name
The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, you can log into that account. But if not explicitly specified the rails app looks for a different role, more particularly the role having your unix username which might not be created in the postgres roles.
To overcome that, you can create a new role, first by switching over to the default role postgres which was created during installation
sudo -i -u postgres
After you are logged in to the postgres account, you can create a new user by the command:
createuser --interactive
This will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user.
Pass over a role name and some permissions and the role is created, you can then migrate your db
Could you have multiple local databases? Check your database.yml and make sure you are hitting the pg db that you want. Use rails console to confirm.
My answer was much more simple. Just went to the db folder and deleted the id column, which I had tried to forcefully create, but which is actually created automagically. I also deleted the USERNAME in the database.yml file (under the config folder).
In Ubuntu local user command prompt, but not root user, type
sudo -u postgres createuser username
username above should match the name indicated in the message "FATAL: role 'username' does not exist."
Enter password for username.
Then re-enter the command that generated role does not exist message.
I ended up here after attempting to follow Ryan Bate's tutorial on deploying to AWS EC2 with rubber. Here is what happened for me:
We created a new app using "
rails new blog -d postgresql
Obviosuly this creates a new app with pg as the database, but the database was not made yet. With sqlite, you just run rake db:migrate, however with pg you need to create the pg database first. Ryan did not do this step. The command is rake db:create:all, then we can run rake db:migrate
The second part is changing the database.yml file. The default for the username when the file is generated is 'appname'. However, chances are your role for postgresql admin is something different (at least it was for me). I changed it to my name (see above advice about creating a role name) and I was good to go.
Hope this helps.
After a bunch of installing and uninstalling of Postgres, here's what now seems to work consistently for me with Os X Mavericks, Rails 4 and Ruby 2.
In the database.yml file, I change the default usernames to my computer's username which for me is just "admin".
In the command line I run rake db:create:all
Then I run rake db:migrate
When I run the rails server and check the local host it says "Welcome aboard".
You might be able to workaround this by running initdb -U postgres -D /path/to/data or running it as user postgres, since it defaults to the current user. GL!

Using Heroku to db:pull a new database

I am trying to learn how to push and pull databases using Heroku's system, I just have a clarifying question.
My existing development database is called project_dev but I want to create a new database. I entered in the following command:
heroku db:pull mysql://root:mydbpassword#localhost/20110302heroku
I have a database.yml file that includes my development, test, and production dbs, and I got this response from Heroku:
Auto-detected local database: mysql://root:mydbpassword#localhost/project_dev?encoding=utf8
Does this mean I have to manually create a new database first if I want to pull from Heroku? Does it mean that I cannot pull at all unless the db is explicitly defined in my database.yml file?
Any pointers would be really helpful. I had a look around on Google, Heroku and SO, but I didn't find the answers I was looking for. Thank you!
Yes, you'll have to first create the new local database but you don't have to declare it in your database.yml file.
When I run heroku db:pull mysql://root:mydbpassword#localhost/newdb it correctly imports into the newdb database. I'm not sure why it auto-detects your local dev database. Do you use the latest heroku and taps gems?

Resources