authentication error for PostgreSQL user? - ruby-on-rails

i am trying to run rake db:create db:migrate db:seed inside a ruby on rails project, i get an error saying FATAL: password authentication failed for user "postgres"
Looking at other similar questions, a lot of people pointed out the validity bug in the pg_hba file, but as far as i can tell, there is no issue with it?
local all postgres md5
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
I also ran into this sudo -u postgres psql -x -c "select * from pg_user where
usename='pand got the following:
-[ RECORD 1 ]---------
usename | postgres
usesysid | 10
usecreatedb | t
usesuper | t
usecatupd | t
userepl | t
passwd | ********
valuntil |
useconfig |
more info : System Ubuntu 14.04 running in a vagrant box, with postgres v9.3.
update: I created a new database and a new user following the instructions found hereenter link description here , Added the new info to the database.yml file found in the config directory just to be sure I am using the DB user no the system user.
update-2: here is the info found in the database.yml file development:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: db1
pool: 5
username: dev1
password: dev1
test:
adapter: postgresql
encoding: unicode
host: localhost
port: 5432
database: db_test1
pool: 5
username: dev1
password: dev1

As there could be quite a number of factors, it could be difficult to point out what is wrong in your setup. I guess I will just try to narrate how I use vagrant + rails + postgresql so I hope this helps you. This assumes that you have a fresh vagrant box with Ubuntu 14.04.
Let's say you have installed the necessary packages for ruby + rails. And now you are going to install postgresql.
sudo apt-get install postgresql postgresql-contrib libpq-dev
And then you would login to your postgresql admin user:
sudo su postgres
psql
Once logged in to the psql console, I would usually create a user named vagrant. I do this because postgresql defaults to using the current shell user as the database user when logging to the db console. This is more of a convenience(for me anyway). And then I would create the database in the console too. After that I give privileges for the database to the user vagrant.
CREATE USER vagrant WITH PASSWORD 'password';
CREATE DATABASE db1;
GRANT ALL PRIVILEGES ON DATABASE db1 to vagrant;
After these steps, I would just add the appropriate database.yml config and run rake db:migrate. I find these workflow effective for me and I never encountered any permission problems with this. I also never touched pg_hba.conf with this setup. Please try this out and I hope this resolves your issues.
Cheers!

Related

Postgresql: "no password supplied" error in rails although login in psql is OK

I created new user 'deploy' for database with certain password and then created database 'deploy_production'.
In my rails app, database is configured as follows:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
production:
<<: *default
database: deploy_production
username: deploy
password: <%= ENV['DATABASE_PASSWORD'] %>
Db password is correctly set in console trough:
export DATABASE_PASSWORD=actual_password
Password is correct because I am able to login with it to psql:
psql -d deploy_production -U deploy
But when I run rails app using Thin, in production enviroment, and the request comes, following error appears:
PG::ConnectionBad (fe_sendauth: no password supplied):
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `initialize'
...
What is interesting - commands RAILS_ENV=production rake db:setup and RAILS_ENV=production rake db:migrate work perfectly.
This is my pg_hba.conf:
local all postgres peer
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Do you have any clues what could cause the problem?
You should setup you env variable in the .bashrc file (or .bash_profile) depending on the linux that you are using.
.bashrc
export DATABASE_PASSWORD=actual_password

error with postgresql datababse : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

When I run the rake db:migrate or run the rails s command, I get the same error:
Error : could not connect to server:
No such file or directory Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I get the error in the browser when I try rails s.
This is my database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: books_development
test:
<<: *default
database: books_test
production:
<<: *default
database: books_production
username: abd
password: <%= ENV['BOOKS_DATABASE_PASSWORD'] %>
Note : I have the databases books_development; books_test
; and the postresql are running without problems when I try sudo /etc/init.d/postgresql start
I did run:
create database books_development;
create database books_test;
in the psql console. And it said that it's done successfully
I tried a lot of solutions and I spent yesterday looking for a solution and no solution in the related questions solved my error.
I have postgresql-9.4 (the latest) and xubuntu 14.04
Any Ideas?
The convention for PostgreSQL packaged for Debian or Debian derivatives such as Ubuntu is to use /var/run/postgresql as the directory for Unix domain sockets. On the other hand the convention for self-compiled postgres client libs is to use /tmp, unless self-configured otherwise.
So the usual root cause of this mismatch between both is a mix of self-compiled client-side stuff with pre-compiled server-side packages (even if client and server are installed on the same machine, client-side and server-side are still distinct and can be out of sync).
Soft-linking from /tmp to this directory as suggested by the asker works except that the link will be lost at every reboot, because in general /tmp is emptied on reboot.
A better option would be to add as an entry in database.yml:
either host: /tmp if the real socket path is /tmp (self-compiled server, packaged client)
or host: /var/run/postgresql if the real socket path /var/run/postgresql/ (packaged server, self-compiled client).
When the value in the host field starts with a slash character, the postgres library knows that it's the location of a directory for local sockets rather than a hostname. The filename inside the directory .s.PGSQL.portnumber is generated and must not be specified, only the directory.
Another possibility is to configure the self-compiled software packages as closely as possible to Debian, overriding the defaults as they do.
I had the same Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”? error when typing psql into the postgres user in Ubuntu 14.04. I could not find an existing working solution.
The short answer for me was: my install made a var/pgsql_socket directory but no configuration files knew about it.
1) Find the postgres.conf file (it was in etc/postgresql/9.6/main for me)
2) change to listen_addresses = '*'
3) add another unix socket directory
unix_socket_directories = '/var/run/postgresql, /var/pgsql_socket' # comma-separated list of directories
4) at this point, sudo service postgresql start attempted to start but did not have authority to create the lock file.
* The PostgreSQL server failed to start. Please check the log output:
2016-10-05 17:14:55 CEST [28472-1] FATAL: could not create lock file "/var/pgsql_socket/.s.PGSQL.5432.lock": Permission denied
2016-10-05 17:14:55 CEST [28472-2] LOG: database system is shut down
5) Change permissions ( found from Mark Berry's comment here )
$ sudo chown root.postgres /var/pgsql_socket
$ sudo chmod g+wx /var/pgsql_socket
6) sudo service postgresql start sudo -i -u postgres psql
That finally worked for me
That means your Postgres server is not running.
Check Postgres Service status from Terminal
sudo service postgresql status
Enable Postgres Service, If not started
sudo service postgresql start
OR
sudo service postgresql restart
Now your command should work, If Postgres Service is successfully started.
I solved It . I Just created a softlink using :
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
and then edited the
/etc/postgresql/9.4/main/pg_hba.conf
( If you have another version of postgresql you have to change 9.4 in the path)
From:
local all postgres peer
To:
local all postgres md5
Solution:
Try this
export LC_ALL="en_US.UTF-8"
and this. (9.3 is my current PostgreSQL version. Write your version!)
sudo pg_createcluster 9.3 main --start
The exact same symptom can be caused by a stale lock file /var/run/postgresql/.s.PGSQL.5432.lock. One of the symptoms of this is psql reporting
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
even though there is clearly a socket with this path available as reported by netstat -lp --protocol=unix | grep postgres
The problem can be solved by removing the lock file and restarting postgresql. This is definitely less invasive than a purge and re-install.
sudo rm /var/run/postgresql/.s.PGSQL.5432.lock
sudo service postgresql restart
On Mac OS X I usually get this error when my computer shuts down incorrectly, for example, due to power failure.
The solution I use is pretty simple and works 100% of the time:
# Find the postgres config folder
cd /usr/local/var/postgres
# remove file
rm postmaster.pid
# restart postgres
brew services restart postgres
Running pg_lsclusters will list all the postgres clusters running on your device
eg:
Ver Cluster Port Status Owner Data directory Log file
9.6 main 5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
if the status is down run
#format is pg_ctlcluster <version> <cluster> <action>
sudo pg_ctlcluster 9.6 main start
If this process is not successfull it will throw the error.
My error was(You can see the error log on /var/log/postgresql/postgresql-9.6-main.log)
FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to the group `ssl-cert`
make sure that postgres is the owner of /var/lib/postgresql/version_no/main
eg: sudo chown postgres -R /var/lib/postgresql/9.6/main/
It happened to me and it turned out that I removed erroneously the Postgres user from "ssl-cert" group. Run the below code to fix the user group issue and fixing the permissions
#set user to group back with
sudo gpasswd -a postgres ssl-cert
# Fixed ownership and mode
sudo chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key
# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgres restart
When I run into this error, my Postgres server was actually listening on a different port (5433) and not 5432.
To solve this, add
port: 5433
to your database.yml file to instruct rails to use the same

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

How to set up Postgres database for local Rails project?

I recently got a new machine and would now like to work on my projects from Github. I'm curious as to how to properly set up the Postgres database on my local machine. I have postgresql, pgadmin3 and libpq-dev installed on Ubuntu (12.04).
I pull down the project:
git clone https://github.com/thebenedict/cowsnhills.git
and run:
bundle.
When I run:
rake db:create && rake db:schema:load
I get this error:
rake db:create && rake db:schema:load
FATAL: password authentication failed for user "cnh"
FATAL: password authentication failed for user "cnh"
....
The config/database.yml file looks like this:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_development
pool: 5
username: cnh
password: cnh
test:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_test
pool: 5
username: cnh
password: cnh
production:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_production
pool: 5
username: cnh
password: cnh
What's the proper way to set up the Postgres database so I can run this project on my local machine?
Right now when I start the Rails server I get:
I came across your question when looking for the same answer. I attempted to follow the instructions #prasad.surase gave you. The problem I found is the ppa repository is going to depreciate soon on 12.04 LTS. Instead I found this link and it really helped.
PostgreSQL setup for Rails development in Ubuntu 12.04
Install postgresql and admin tools through the package manager
sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3
Login to postgresql prompt as the postgres user
sudo su postgres -c psql
Create a postgresql user for your project
create user username with password 'password';
Setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser
alter user username superuser;
Create the development and test databases
create database projectname_development;
create database projectname_test;
Give permissions to the user on the databases
grant all privileges on database projectname_development to username;
grant all privileges on database projectname_test to username;
To end the postgresql session type \q
Update password for the user
alter user username with password ‘new password’;
firstly, install postgresql
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
#now install postgresql
sudo apt-get install postgresql-9.1 libpq-dev
create a new user in psql
sudo su postgres
createuser user_name #Shall the new role be a superuser? (y/n) y
Gemfile
gem 'pg'
bundle install
development.yml
development:
adapter: postgresql
database: app_development
pool: 5
username: user_name
password:
You follow this link http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
to create a postgres user and replace the credentials 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.

Resources