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
Related
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.
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.
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'
It's taken me two days to get this far. But I'm almost there. When I run cap deploy:cold the deployment fails with this message:
servers: ["xxx"]
[xxx] executing command
** [out :: xxx] rake aborted!
** [out :: xxx] FATAL: password authentication failed for user "my_app"
My database.yaml has this:
production:
adapter: postgresql
encoding: utf8
database: my_app_production
pool: 5
host: localhost
username: my_app
password: secret
I'm running cap deploy under a user I created called deployer. I installed Postgres under deployer. I also created the user my_app in psql:
create user my_app with password 'secret';
create database my_app_production owner my_app;
I verified that the user my_app exists by running \du. When I ssh through deployer#xxx and I run the command psql I get psql: FATAL: role "deployer" does not exist.
What am I doing wrong?
For the psql invocation to be similar as what rails does, it should be:
psql -h localhost -U my_app -d my_app_production
If you omit these options, psql will take by default:
the OS username as the database username
the OS username as the database name
a Unix socket directory as the host (which differs from localhost and generally has different authentication rules in pg_hba.conf)
I've gone through the very same Railscasts you are going through I think. I just got my site up with it, though I am still having a couple glitches with cap deploy. In any case, for clarity.
You created a deployer user on the remote host obviously. But, that doesn't mean "deployer" is a user in Postgres. Once you ssh as deployer#xxx.xxx.xxx.xxx you have to run sudo -u postgres psql to enter psql as the default postgres admin. Presuming you're using Ubuntu, there is no default password.
Where I'm confused is how, if you can't get in on the remote system with deployer, did you create the my_app user in psql there? It almost sounds like you did this locally (Vagrant maybe?) and didn't do it remotely. As I said, I think I just finished the same stuff you're looking at and it works if the my_app user is created on the remote box with the correct password.
OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword