Rake aborted after FATAL no pg_hba.conf entry - ruby-on-rails

Trying to run my Rails 4.0.4 (Ruby 2.1.1) application on CentOS 6.5
Failing when I attempt to migrate:
$ rake db:migrate
rake aborted!
PG::ConnectionBad: FATAL: no pg_hba.conf entry for host "::1", user "my_user", database "my_database_dev", SSL off
I am not sure how to tackle this one. On my mac setup all worked fine without having to touch the pg_hba.conf.
Any lead is welcome.
I have created my_user as follow:
CREATE ROLE my_user WITH CREATEDB SUPERUSER LOGIN;
And allowed my_user to access all database with 'trust' in the pg_hba.conf
my config/database.yml contains
development:
adapter: postgresql
host: localhost
encoding: unicode
database: my_database_dev
pool: 5
username: my_user
password:
template: template0

::1 is localhost in ipv6. Your PostgreSQL config doesn't have a config for v6. Try setting your server to 127.0.0.1 the ipv4 address of localhost.

It looks like rake is for some reason passing a bad host, as I assume ::1 is not the name of your server (it looks more like an IP mask of sorts from the Postgres config). (Edit: ::1 is IPv6 localhost, per Doon's comment below.)
Not sure offhand why the same code would work OK on your Mac, but I assume there's a database.yml involved in your Rails config? You may want to try specifying a host attribute in there (it can point to localhost if your server is local) and see if that helps.
i.e.
host: localhost
You'd also want to include one for the port if Postgres is not on the standard port for your Postgres install (5432 with a default install).

Related

Rails development environment trying to connect to AWS database, not localhost's PostgreSQL

I have an existing Rails app which was previously running successfully on my local laptop (running OSX) with the database stored locally in PostgreSQL. I've also successfully deployed the Rails app to Elastic Beanstalk with the database in RDS - the hosted site is still running.
I haven't touched the localhost version for months and tried to start it today using rails server -e development, but when visiting http://localhost:3000/ as per usual, I get a PG error message
PG::ConnectionBad at / FATAL: password authentication failed for user
"murjfphxxxxxx" FATAL: no pg_hba.conf entry for host "115.x.x.x",
user "murjfphxxxxxx", database "d37vsvehxxxxxx", SSL off
The same error is thrown in the Rails console. It seems that the development version is trying to connect to the AWS database, rather than the localhost database, but I can't figure out why.
/config/database.yml lists the development environment as:
development:
adapter: postgresql
encoding: unicode
database: <my-app>_development
host: localhost
pool: 5
username: postgres
password: <password>
I can connect to the localhost PG database via PSQL and see the contents, so it appears that the localhost PG database is valid.
Why would the development environment try to connect to the AWS version of the database? Please let me know if you need any further information to debug this.
Using ruby-2.3.1, Rails 4.2.0, PostgreSQL 9.6.0.0
You probably have DATABASE_URL in your env. You can check by typing env | grep DATABASE_URL in terminal.
If so, try unsetting it with unset DATABASE_URL.
As said in documentation:
If you have both config/database.yml and ENV['DATABASE_URL'] set then
Rails will merge the configuration together. To better understand this
we must see some examples.
When duplicate connection information is provided the environment
variable will take precedence.

Rails: fe_sendauth: no password supplied (PG::ConnectionBad) from Ruby, but ok in Rails

I'm having problems assessing a postgres database from straight ruby.
I've created a Postgres database using Rails
>rails new www --database=postgresql
using Rails 4.2.5 and Postgres is 9.4
It produces the following config/database.yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: www_development
test:
<<: *default
database: www_test
production:
<<: *default
database: www_production
username: www
password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
I can run rails server, db:drop, db:create and db:migrate fine.
I can also access the database fine with psql
>psql www_development
But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.
It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' pg_hba.conf file.
At wits end trying to figure this one out. Any help would be much appreciated.
app.rb
require 'yaml'
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?
as per documentation peer auth does
Obtain the client's operating system user name from the operating
system and check if it matches the requested database user name.
That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...
Options:
put username: postgres to ActiveRecord::Base.establish_connection(
change local all all peer to local all all trust
With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.
development:
<<: *default
database: postgres
username: postgres
password: postgres
# must specify the right DB, superuser and superuser Password as per postgreSQL setup
This worked for me, the only changes I needed to make. (ok fine i re-installed pgsql with 12.1)
I had this same issue when setting up a Rails 6 application.
The issue was that when start the rails server using rails server, and try to view the application from a browser, I get the error below:
ActiveRecord::ConnectionNotEstablished
fe_sendauth: no password supplied
Here's how I solved it:
The issue was caused by me not specifying/supplying the database password to be used by the application, so when the application tries to connect to the database specified in the config/database.yml it runs into that error.
I solved it by specifying the database password for the application in the config/database.yml:
# The password associated with the postgres role (username).
password: my-password
Also, ensure that you've created the database for the application if you've not created it already using:
rails db:create
Afterwhich I restarted the rails server:
rails server
This time when I tried viewing the application from the browser, it worked fine.
That's all.
I hope this helps
I was trying to use peer authentication which shouldn't ask for the password.
For me the issue was that I was specifying localhost in the database.yml so that was forcing the database driver to try to make a different type of connection (e.g., tcp, named pipe, etc)
Removing the "host: 'localhost'" line from my config solved the problem for me per this other answer: ActiveRecord connection to a localhost postgresql database in trust mode
Note that in my case I'm using the 'peer' method and not the 'trust' method but the solution is the same

Ruby on Rails - Postgresql using md5 - Rake aborted! fe_sendauth: no password supplied

I'm setting up Postgresql with Rails on Ubuntu and I am getting the error: Rake Aborted! fe_sendauth: no password supplied when running rake commands.
I have the following in my database.yml file:
development:
adapter: postgresql
encoding: unicode
database: test_database
pool: 5
username: postgres
passsword: <password>
I have changed the method of connection in the pg_hba.conf file and restarted the postgresql server however the same error occurs.
# TYPE DATABASE USER ADDRESS METHOD
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
I know I can change the method to trust and run the rake commands but I need to use md5 since I want to use thinking sphinx and it doesn't support trust.
I can connect to the database through pgAdmin and in the console with my username and password: psql -U postgres -d test_database.
Am I missing something?
[EDIT]
Turns out I cannot speel. I changed 'Passsword' to 'Password' in my database.yml and it works now...
Fixed typo, when wanting to use user/password authentication in rails on ubuntu you must change the method access type in the pg_hba.conf of your postgresql installation directory to md5

ROR change application database from SQLite to PostgreSQL

I have a web application which uses SQLite. I deploy it on heroku which uses PostgreSLQ. This causes problems sometimes and I was advised to develop my app using PostgreSQL instead of SQLite.
I found out that I should modify database.yml like that (same for test and production):
development:
adapter: postgresql
database: my_database
username: my_username
password: my_passwod
host: /var/run/postgresql or localhost
Well the only database I've ever used is SQLite, so I just tried to take my chances, but failed. I filled this file with some random data.
rake db:migrate resulted in:
When I used host: localhost
> could not connect to server: Connection refused Is the server running
> on host "localhost" and accepting TCP/IP connections on port 5432?
When host: /var/run/postgresql
> 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 suppose I should start PostgreSQL server first, but have no idea how to do this. Please give me a step by step answer how to move from a SQLite application to a working PostgreSQL application.
I would like to advise to you that you should download Postgresql including the PGADMIN itself which is easier to use than the psql terminal.
And I think when you download/install Postgresql from their official website... the package was complete already.
Upon installing, the postgresql will ask you a certain password that you will be using in accessing your postgresql server.
After the installation, open the PGADMIN and connect to the server. Enter your password (which you had declared during installation).
If you can't connect to the server, then edit the port. To do this, right click the server then go to properties... edit the port into something which is free. Example: 5433 and so on. It's up to you.
If everything's finally working... setup the correct config for your database.yml
This is important:
development:
adapter: postgresql
database: name_of_database_here
host: localhost
username: postgres
password: your_db_server_password_here
pool: 5
timeout: 5000
port: 5433
Okay from that config info above, specify the important parts. By default, your db server username is postgres and obviously your host is localhost because you are setting up under the development.
If your port is 5432 by default then just remove the port part.
Let's go to your gemfile.
In order for you to deploy your app in heroku. Use gem 'pg' instead of sqlite3.
If you have an existing sqlite3 database then put the gem inside the development group. In that case, Heroku will successfully bundle during git push heroku master process.
group :development do
gem 'sqlite3'
end
Your gem 'pg' can either go outside the groups or put it in your production group.
Important:
Before any deployment procedure, make sure that you can run the app locally (localhost). Then if everything's working... that's the time that you should organize the necessary stuffs appropriately.
If you wish to switch to Postgresql instead of sqlite3 after pushing the app to Heroku... you can do so by pgbackups add-on and pg_restore the dump file into your local postgresql db server.
That's it. Hope it helps.
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Error when trying to create database with Rails, rake db:create

I'm using Ruby1.9 and Rails 2.3.4 and I have mysql-ruby (2.8.1) gem installed.
when I try rake db:create I get the following
Couldn't create database for
{"adapter"=>"mysql",
"database"=>"war_development",
"username"=>"root", "password"=>nil,
"host"=>"localhost"}, charset: utf8,
collation: utf8_unicode_ci (if you set
the charset manually, make sure you
have a matching collation)
My database.yml has the following for development
development:
adapter: mysql
database: war_development
username: root
password:
host: localhost
Any ideas what's going wrong?
Thanks,
Tam
Are you allowed to use the root user without password? Is the mysqlserver listening on 127.0.0.1 or localhost?
Strange to see some behaviour changed, and I don't know if it is specific to snow leopard. I use tiger and got the same error. I added
host: 127.0.0.1
to the following (ofcourse with a correct mysql root/pw)
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password: *********
socket: /tmp/mysqld.sock
And it worked (on tiger/gem mysql-2.8.1/mysql Ver 14.14 Distrib 5.1.30). Hope it helps.
I followed the steps in another SO thread. However, the command for my installation is different from that of there, as I am not upgrading an existing db but installing a fresh one. So instead of advised one
$ sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
I typed the following
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/bin/mysql
PS: host: localhost remains as is, no need to change to 127.0.0.1
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql/bin/mysql
works for me. Thank you!
You will check out your usrname and password whether its correct or not.
Also you will check out whether your mysql server is working correctly.
and also some time found the problem of socket path most of the time its is inside /tmp directory but sometimes it may not be. so try with with the default path.
Hopefully if you will try this it will work for you.

Resources