Ruby on Rails - db:create:all error - ruby-on-rails

When I am trying to create a new rails app with a postgres db I cannot complete the rake command.
This is what I have done.
sayth#sayth-TravelMate-5740G:~$ sudo -u postgres psql postgres
[sudo] password for sayth:
psql (9.2.4)
Type "help" for help.
postgres=# create role sayth login createdb;
ERROR: role "sayth" already exists
postgres=# DROP user sayth;
DROP ROLE
postgres=# create role sayth login createdb;
CREATE ROLE
postgres=# initdb -D /usr/local/pgsql/data
postgres-# \q
sayth#sayth-TravelMate-5740G:~$
I have created my rails app with
rails new testapp2 -d postgres
realised after watching Railscast why it didn't work http://railscasts.com/episodes/342-migrating-to-postgresql
So I edited the config/database.yml
development:
adapter: postgresql
encoding: unicode
database: testapp2_development
pool: 5
username: sayth
password:
test:
adapter: postgresql
encoding: unicode
database: testapp2_test
pool: 5
username: sayth
password:
But still rake wont create my database.
sayth#sayth-TravelMate-5740G:~/testapp2$ rake db:create:all
FATAL: Peer authentication failed for user "testapp2"
...
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"testapp2_production", "pool"=>5, "username"=>"testapp2", "password"=>nil}
Seems I have read a lot and tried anything I have found but unsure what to do.
This command fails as well.
sayth#sayth-TravelMate-5740G:~/testapp2/config$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"

the error is only in creating production database, here you have
specified wrong user, 'testapp2' instead of 'sayth'

Related

ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist when I try to migrate bd in rails

I cloned a friend's git repo and I'm trying to migrate the db. I started postgres, but when I run rails db:migrate, I keep getting the errors:
Rails Error: Unable to access log file.
and
ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist
I've tried all available solutions online but keep getting the same error. Does anyone know what I'm doing wrong?
Try the following
Setting Up Postgres
Create a Postgres user for the Rails app we'll create in the next step. To do this, switch into the Postgres user:
su - postgres
Then create a user (or a "role", as Postgres calls it):
create role myapp with createdb login password 'password1';
and make sure you have config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
The solution for me was:
createuser -s postgres
After I ran that on the command line my rake task to create the DB worked

Do I need to create a new Postgres user for every new Rails app?

On my OS X machine, I have a working Rails app (4.2) with Postgres (9.3).
Now, to create a second Rails app, would I need to create a new pg user?
In my first app, no username is provided in database.yml
development:
adapter: postgresql
database: my_first_app_development
pool: 5
timeout: 5000
Short answer no. You can use one user for many databases if that user be owner of this database or has superuser role.
I prefer to use one user in local machine that have superuser privileges.
$> sudo -u postgres psql
postgres# CREATE ROLE my_user CREATEDB SUPERUSER;
postgres# ALTER ROLE my_user WITH LOGIN;
Now you can create database with my_user owner:
postgres# CREATE DATABASE my_database WITH OWNER my_user;
Or just create database with a rake task but before add username to database.yml:
database: my_first_app_development
username: my_user
from shell:
$> bundle exec rake db:create
This good only for local development environment for production create separate user with right privileges.

PG::ConnectionBad: FATAL: password authentication failed for user "alphauser"

I'm working on an application in Rails for my college. The application was started by the students from previous year and now it's me and my colleagues turn to continue work on it. I took the application from github, I run bundle install, but when to run rake db:migrate I got this PG::ConnectionBad: FATAL: password authentication failed for user "alphauser".
In database.yml I have these
development:
adapter: postgresql
encoding: unicode
database: alpha_database
host: localhost
pool: 5
username: alphauser
password: alphapassword
I don't know what to do in this case.
You have to create corresponding user and database manually like this:
in shell:
psql
then:
create user alphauser with password 'alphapassword';
create database alpha_database owner alphauser;
alter user alphauser superuser createrole createdb replication;
\q
don't forget semicolons.
Try to use:
sudo -u postgres createuser --interactive --pwprompt
to add the role and the password
So the problem is with your rails application using the database.yml file to try and connect to your local database and failing to find the user alphauser (since I assume you're using different computers/environments as the previous students).
Databases have users similar to applications, the postgres documentation is pretty dense on this, but I would guess if you can create a user alphauser, and make it's password alphapassword then you will have a new clean database for your app that you can run rake db:migrate on.
Postgres docs: http://www.postgresql.org/docs/9.2/static/app-createuser.html
Try running this command from the command line createuser -P -s -e alphauser
This will prompt for a password, which is alphauserpassword
create user alphauser with password 'alphapassword';
Then
rake db:setup

Rails: Postgres permission denied to create database on rake db:create:all

I am trying to create postgres databases for development and tests. I'm using:
OSX Yosemite
Rails version: 4.2.0
git version: 2.2.2
psql version: 9.4.0
ruby version: 2.1.0p0
HomeBrew version: 0.9.5
Gemfile:
gem 'pg'
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: myapp_development
username: username
password:
test:
<<: *default
database: myapp_test
rake db:create:all returns
PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "myapp_development" ENCODING = 'unicode'
.... (lots of tracing)
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_development", "username"=>"username", "password"=>nil}
myapp_test already exists
What is wrong?
EDIT
I just tried changing the username in the database.yml to my username that I'm using on my Mac. It worked. It also told me that not only maybe_test already exists, but it also just told me that myapp_development already exists too.
Why wouldn't it be able to use the other username that I had created and assigned a role to CREATEDB?
Why did it say that the development couldn't be created then tell me that it already existed?
This all seems way too confusing and reminds me of PHP setup with Apache back in the very old days. I don't want to have to deal with problems every time I create a new app and try to follow the Heroku recommendations to use PostgreSQL during development too.
I have faced same issues when running rake db:test:prepare in postgresql on my Ruby on Rails project. This is pretty clear from the error message, that its a permission issue for the user. I added CREATEDB permission for new_user as following from the console.
To access postgres console:
$ sudo -u postgres -i
postgres#host:~$ psql
In there:
postgres=# ALTER USER new_user CREATEDB;
It's working perfect for now. You may have another issues with database ownership, for this you can change database privileges and owner as following command.
postgres=# GRANT ALL PRIVILEGES ON DATABASE database_name to new_user;
postgres=# ALTER DATABASE database_name owner to new_user;
Looking at your schema your credentials for development and test are different.
Perhaps remove username and password from the schema, seeing that your test database did get created.
create database demo;
create user demotest with password '123';
grant all privileges on database demo to demotest;
commit;
This is script for creation of database. But any existing database having password '123' then change your password for new database to password '1234'. This procedure working for me.

Postgresql or psql 'rails db' password error

I am trying to prompt the psql interface to try to create a database, actually following from Dr. Hartl's tutorials http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=3.2.
I created the project with:
rails new postgr_ --database=postgresql
I added passwords to the database.yml file:
development:
adapter: postgresql
encoding: unicode
database: postgr__development
pool: 5
username: postgr_
password: 12345
test:
adapter: postgresql
encoding: unicode
database: postgr__test
pool: 5
username: postgr_
password: 12345
production:
adapter: postgresql
encoding: unicode
database: postgr__production
pool: 5
username: postgr_
password: 12345
I then enter into terminal:
$ rails db
And I get the following error after entering my password:
psql: FATAL: password authentication failed for user "postgr_"
I've been going at this a good part of yesterday and all day today and was unable to work around this. I may very well be missing something fundamental, if you spot it please let me know. Thank you!
You're missing the steps to setup the postgresql role and database creation.
This procedure depends on the system you are using. I will assume that you are using a mainstream linux distribution.
First login to the postgresql account. You may use one of the following commands:
$ su - postgres
or
$ sudo -i -u postgres
Once logged in, start the psql program:
$ psql template1
At the psql prompt, create a new user role and a database for your project:
=> create role postgr_ with createdb login password '12345';
Then simply quit the program
=> \q
And logout from the postgresql user account
$ exit
Then you should be able to run the rail db command successfully

Resources