Error while creating rails Database - ruby-on-rails

I'm trying to start a rails projec, when I try to create the database with "rake db:create" y recive the following:
postgresql_adapter.rb:898:in 'rescue in connect': FATAL: database "Real_Estate_development" does not exist
Run '$ bin/rake db:create db:migrate' to create your database (ActiveRecord::NoDatabaseError)
My database.yml contains:
development: &default
adapter: postgresql
database: Real_Estate_development
username: "ruby"
password: "ruby123"
encoding: utf8
host: localhost
min_messages: warning
pool: 2
timeout: 5000

Try psql -U ruby -d postgres -W ruby123 to confirm the user have access right.
Maybe $ bin/rake db:create solve the problem as the error message suggests.

Related

Can't run "rake db:create"

I try to setup a postgres DB for my rails project but everytime I run the command:
rake db:create
I get the error
FATAL: role "myrole" does not existCouldn't create database for{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5,
"database"=>"web_db", "username"=>"myrole", "password"=>"password", "host"=>"localhost"} rake aborted!
When I login into my web_db database with the user myrole and type in \du it showsme all the roles including myrole! This role has also Superuser, create Role and createDB attributes.
I have no idea whats wrong..
Edit my database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myrole
password: password
host: localhost
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myrole
password: password
host: localhost
What I did wrong:
I used su postgres to open psql and create roles.
What I had to do is to login from my user with the command:
psql -h localhost -d web_db -U postgres or sudo -u postgres psql
Then I had to create a role and asign rights:
CREATE ROLE myrole;
ALTER ROLE myrole WITH login;
ALTER ROLE myrole WITH CREATEDB;
ALTER ROLE myrole WITH PASSWORD 'password';
then I could run the command : rake db:setup

Cloud9 + rails + Postgresql usage

I can not set up a Rails app using Postgresql for development on Cloud9 (c9.io): the migration does not succeed.
Common error:
~/workspace (master) $ rake db:migrate
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Cloud9 does not run PG by defalut. Below is the fast & easy way I use to use Postgresql on C9:
1.
Gemfile.rb:
gem 'pg'
2.
Database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: my_name
password: my_pass
host: <%= ENV['IP'] %>
development:
<<: *default
database: my_db_development
test:
<<: *default
database: my_db_test
production:
<<: *default
database: my_db_production
Paste the following code alltogether in the console:
`
sudo service postgresql start
sudo sudo -u postgres psql
CREATE USER my_name SUPERUSER PASSWORD 'my_pass';
\q
echo "export USERNAME=my_name"
echo "export PASSWORD=my_pass"
source
bundle
sudo sudo -u postgres psql
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\c template1
VACUUM FREEZE;
\q
bundle exec rake db:create
rake db:migrate
Done!
However after not using the app for some hours the db goes to sleep & you have to "switch" Postgres on manually by typing in the console:
sudo service postgresql start

Cloud9 postgres

I am trying to set up a postgres database in a Rails app in Cloud9.
I have followed the instructions here: https://docs.c9.io/setting_up_postgresql.html and set up a database called cc_database.
My database.yml file looks like this:
development:
adapter: postgresql
encoding: SQL_ASCII
database: cc_database
pool: 5
username: postgres
password: password
When I run rake db:setup I get the following error:
PG::ConnectionBad: FATAL: Peer authentication failed for user "postgres"
I am quite new to all this, so any advice would be much appreciated.
Do the following steps:
Create a new username and password for postgresql on cloud9:
$ sudo service postgresql start
$ sudo sudo -u postgres psql
postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
postgres=# \q
Create ENV variables on cloud9:
$ echo "export USERNAME=username" >> ~/.profile
$ echo "export PASSWORD=password" >> ~/.profile
$ source ~/.profile
My database.yml for rails 4.2.0 on cloud9:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
host: <%= ENV['IP'] %>
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
production:
<<: *default
database: sample_app_production
Include the gem pg in Gemfile and install:
gem 'pg', '~> 0.18.2'
$ bundle install
Update template1 postgresql for database.yml on cloud9:
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
postgres=# DROP DATABASE template1;
postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
postgres=# \c template1
postgres=# VACUUM FREEZE;
postgres=# \q
From command line run:
bundle exec rake db:create
The postgresql in cloud9 is setup to authenticate with peer when localhost connection.
So the quickly resolution is change the user in your database.yaml to current user. In my case the name of user is ubuntu.
To see your current user use the command
$ echo $USER
So a list of command in terminal is .
$ sudo su - postgres
$ createuser ubuntu -dslP
$ Enter password for new role: **same password from your yaml file**
$ Enter it again:
Set your yaml file like this
development:
adapter: postgresql
encoding: SQL_ASCII
database: cc_database
pool: 5
username: ubuntu
password: password
Now you can run
rake db:create
rake db:migrate
Use the username "ubuntu" with a blank password. My database.yml looks like this:
development:
adapter: postgresql
encoding: unicode
database: myflix_development
pool: 5
username: ubuntu
password:
test:
adapter: postgresql
encoding: unicode
database: myflix_test
pool: 5
username: ubuntu
password:
If it then complains the password is wrong, try changing the password using Cloud9's instructions:
In the command line, type: sudo sudo -u postgres psql
postgres=# \password
Enter new password: leave it blank and press enter
Enter it again: leave it blank and press enter
postgres=# \q
I'm pretty new to this to. Hope that works!
How to setup PostgreSQL & Rails on Cloud9
At time of writing, Cloud9 has PostgreSQL pre-installed, so you won't need to install it yourself. However, its not running by default, so you will need to start it with this command in the terminal:
sudo service postgresql start
Change the PostgreSQL password to 'password' (or choose a different password):
sudo sudo -u postgres psql
# This will open the psql client.
# Type \password and press enter to begin process
# of changing the password:
postgres=# \password
# Type your new password (e.g. "password") and press enter twice:
Enter new password:
Enter it again:
# Password changed, quit psql with \q
postgres=# \q
Edit your config/database.yml to be:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
# Important configs for cloud9, change password value
# to what you entered in the previous psql step.
template: template0
username: ubuntu
password: password
development:
<<: *default
database: your_app_name_development
test:
<<: *default
database: your_app_name_test
production:
<<: *default
database: your_app_name_production
username: your_app_name
password: <%= ENV['YOUR_APP_NAME_DATABASE_PASSWORD'] %>
(Note the template, username, and password configs in the default section above are essential).
Add the pg gem to your Gemfile:
gem 'pg'
Run bundle install.
Remove the sqlite gem from your Gemfile (and optionally delete the db/*.sqlite3 files).
Create the database, load schema.rb, and seed the database using the db:setup task:
bundle exec rake db:setup
# Run bin/rake -AD db to see all db-related tasks
Start or restart your rails app and check it is working.
Note, the non-seed data from your old sqlite database will not be present in the new database.
If you ever want to use the psql client to interact with PostgreSQL directly, in the terminal run psql or run bin/rails db.
For me, doing the steps in Cloud9 workspace setup with Rails and Postgresql wasn't enough.
It was passing in my user, but not the password.
echo $USERNAME was coming up with nothing.
Solution
$ sudo su - postgres
$ createuser ubuntu -dslP
Then I did this:
sudo sudo -u postgres psql
postgres=# \password
Enter new password: entered a real password
Enter it again: entered it again
postgres=# \q
Then I did this in my yaml file (note I killed the host part):
development:
adapter: postgresql
encoding: unicode
database: my_database_name
pool: 5
username: ubuntu
password: actual_password
Then I was able to create my database with:
rake db:create
And my Rails server started without any more hiccups.
Found the solution. Needed to edit the pg_hba.conf file to change the authentication from peer to md5 like this:
local postgres postgres md5
It's hard to find the file as it can only be accessed via the terminal in cloud9. You cannot find it in the file tree.
If you type the following into postgres it will show you the location
SHOW hba_file;
You can then find and edit in in vim via the terminal.
A short version when you have rails app prior to creating any databases:
add the gem 'pg'
remove gem sqlite3
$ bundle install
$ gem install pg
Then,
$sudo service postgresql start
$psql -c "create database myapp_development owner=ubuntu"
as per https://community.c9.io/t/how-do-i-set-up-postgresql-on-c9-for-my-rails-app/2614/4
Changing myapp for whatever name. Then, copy-paste below, changing myapp for whatever name.
/myapp/config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: ubuntu
password:
timeout: 5000
As above comments, no password needed, username can stay ubuntu.
$rake db:migrate
If you're using heroku, you won't need the production section in database.yml

Rails Connect with Postgresql database in application ubuntu

I am using ubuntu 12.04 and rails 3.2. I am creating a rails application in which I'm using PostgreSQL databse. I installed postgresql using the following command:
sudo apt-get install postgresql
for reference i checked out https://help.ubuntu.com/community/PostgreSQL. Later I created the user postgres and set the password postgres using the following command
sudo -u postgres psql postgres
\password postgres
Next I created the database using:
sudo -u postgres createdb mydb
I tried to connect with Postgresql with the username postgres and password postgres and got successfully connected with the following command:
psql -U postgres -h localhost
Password for user postgres:
psql (9.1.4)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
postgres=#
In my rails app my database.yml has the following code:
development:
adapter: postgresql
encoding: unicode
database: mydb_development
pool: 5
username: postgres
password: postgres
test:
adapter: postgresql
encoding: unicode
database: mydb_test
pool: 5
username: postgres
password: postgres
production:
adapter: postgresql
encoding: unicode
database: mydb_production
pool: 5
username: postgres
password: postgres
Now when I run the command rake db:migrate i get the following error:
rake aborted!
FATAL: Peer authentication failed for user "postgres"
I tried adding host: localhost to database.yml for each environment and i get the following error:
rake aborted!
couldn't parse YAML at line 28 column 0
The line 28 is
development:
adapter: postgresql
encoding: unicode
database: hackathonio_development
pool: 5
username: postgres
password: testing
host: localhost {This is line 28}
Please help me figure out a solution for this..
I think you may have 2 problems. First, the host not being set as Shreyas pointed out. But I believe the second problem is that when you set the hostname Rails is trying to connect to PostgreSQL via a tcp socket and not a local ruby socket. My guess is you need to modify your pg_hba.conf file to allow postgres to login via localhost. Below are a few SO questions with answers that may help.
Rails can't login to postgresql - PG::Error - password - Correct info
What's the difference between "local" and "localhost" connection types in pg_hba.conf?
Can't use postgres user in new database for rails 3 on ubuntu 10.04 server
Please add host to your yml as localhost
My recommendation:
Step 1
Create a new, different user by running
$ createuser <username>
on the command line. You'll be prompted for password, etc. (Examples/docs: http://www.postgresql.org/docs/8.1/static/app-createuser.html)
Step 2
Update database.yml with the new users's username/password. Forget the first user you created, at least for now.
Step 2
$ rake db:create
Step 3
$ rake db:migrate
I think those steps are more likely to work that what you're trying.

Production database not created by rake db:create command

Im a Rails beginner and am using Rails 3 on Ubuntu 10.10. My database.yml is as follows.
development:
adapter: mysql
database: project_dev
username: root
password: rootpassword
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: project_test
username: root
password: rootpassword
host: localhost
production:
adapter: mysql
database: project_production
username: root
password: rootpassword
host: localhost
Then i switched to the project folder and ran the command:
rake db:create
But,only the project_dev and project_test databases were created. The project_production database did not exist in mysql. What could the problem here ?
Please Help
Thank You
That is the way it is intended to be. To create the production database do:
RAILS_ENV=production rake db:create
Also, have a look at rake db:setup which will run anything you put in db/seeds.rb.
This is by design as #iain suggests. To create all databases, run rake db:create:all.

Resources