java.sql.SQLException: FATAL: no pg_hba.conf entry for host - ruby-on-rails

Developing RoR app on Windows using RubyMine, trying to connect to Postgres database hosted on Heroku server. No Postgres installed locally. Getting "java.sql.SQLException: FATAL: no pg_hba.conf entry for host..." What to do, what to do?

This is due to Heroku databases needing additional SSL configuration.
You need to enable SSL in your jdbc string by appending:
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
For details see Heroku's help document.

It looks like this is a RubyMine issue. RubyMine will convert the hostname to an IP address and pg_hba.conf includes the hostname but not the IP address hence the error. It works fine from the command line (e.g. rails s).

Related

heroku pg:psql not working [connection refused]

I am running a Ruby on Rails app on Heroku with Postgres Database.
I used to be able to connect to my database from my terminal using heroku pg:psql command
Recently, things have changed. After running heroku pg:psql command, I get the following error.
--> Connecting to postgresql-perpendicular-94363
psql: could not connect to server: Connection refused
Is the server running on host "ec2-23-23-130-158.compute-1.amazonaws.com" (23.23.130.158) and accepting
TCP/IP connections on port 5432?
I tried to search online about it, but nothing useful has been found.
Please help. Thanks 😄
I've just tried connect to your database:
psql "dbname=postgresql-perpendicular-94363 host=ec2-23-23-130-158.compute-1.amazonaws.com user=test_user port=5432 sslmode=require"
And I've got:
psql: FATAL: password authentication failed for user "test_user"
Because, of course I don't know you credentials :)
However, it seems that connection works. I suppose, that you forgot set sslmode. As you can see in documentation, it is required:
All connections require SSL: sslmode=require.
Update:
I've checked connection without sslmode, and message is other than yours:
FATAL: no pg_hba.conf entry for host "31.42.24.63", user "test_user", database "postgresql-perpendicular-94363", SSL off
So, I suppose that this is a problem with you internet connection. Check you firewall setup.
As per the description mentioned in the post, the way you have specified is correct.
Moreover you can also try the below mentioned command where you specify the database as well.
heroku pg:psql DATABASE_URL
Note: This is only used when there are multiple databases linked.

Why can't I migrate my postgres database in windows?

I switched my database from sqlite to postgres for deployment on heroku. When I did that and attempted to run heroku run rake db:migrate -a I saw this error:
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "pay_date" cannot be cast automatically to type date HINT: You might need to specify "USING pay_date::date". : ALTER TABLE "recurring_payments" ALTER COLUMN "pay_date" TYPE date
So i created a migration to fix this, but when I run rake db:migrate to run the migration, I see this error:
PG::ConnectionBad: 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"?
However, when I check my services, I see that it is running, and it is configured to listen on port 5432. What else could be causing this issue?
Also, if I add this line to database.yml:
host: localhost
The error changes to this:
PG::ConnectionBad: could not translate host name "localhost" to address: Name or service not known
Perhaps this is more promising?
You seem to have multiple problems:
By default Rails uses Unix sockets to connect to PostgreSQL. Hence the original error message.
Adding the host key switches Rails to a TCP/IP connection instead, which is what you want in Windows.
Now you seem to have another problem: localhost cannot be resolved to an IP address, which suggests that you have general configuration problems on your Windows machines which are unrelated to Rails or PostgreSQL.
Check if your hosts file is messed up or if your (personal) firewall is blocking Rails or PostgreSQL. Try running ping localhost.
If all else fails try using host: 127.0.0.1 instead.

cannot connect to Heroku Postgres - error "server closed the connection unexpectedly"

I am trying to use the Heroku CLI to connect a Postgres database in the cloud.
heroku pg:info shows the database information, but heroku pg:psql fails with the following error:
$ heroku pg:psql
--> Connecting to postgresql-<database-id>
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
What's wrong?
Port 5432 must be unblocked for psql to work and I was unable to do that. Switching to a different network (e.g. Internet provider at home or mobile Internet) may help.
Might not be the issue for everyone, but if you have a computer set up to connect with mTLS by having a ~/.postgresql/postgresql.crt and ~/.postgresql/postgresql.key, psql will automatically send the key up to all databases you connect to, and you will break heroku pg:psql connections to other databases
So just
mv ~/.postgresql{,.bak}
and try again. If that's your issue, you'll want to put your mTLS certs in a different directory and manually pass them as arguments or via config vars
The location of the certificate and key files can be overridden by the
connection parameters sslcert and sslkey or the environment variables
PGSSLCERT and PGSSLKEY

Configure RubyMine remote connection to Heroku PostgreSQL

How should I configure RubyMine jdbc datasource to remotely connect to Heroku's PostgreSQL database? Currently I am using such connection URL:
jdbc:postgresql://ec2-54-197-241-67.compute-1.amazonaws.com/dbqi9t12t5035q
but I get error about lack of entry for host '62.87.242.2' in pg_hba.conf. I am pretty sure that user and password are correct, because it runs online... :-). Could you help me?
Thank you in advance!
In the Advanced tab, there are a bunch of properties. Set the following properties:
ssl -> true
sslfactory -> org.postgresql.ssl.NonValidatingFactory
The error you're seeing means the security policy of the Postgres server doesn't allow the connection. Heroku Postgres only allow connections from within Heroku, as far as I know.
You'll need to use their official command line tools if you want to open a remote database connection:
heroku pg:psql
The official tools authenticate to Heroku and tunnel through their network.

Postgresql, problems after updating gem

I just updated my gems. And now I have problems connecting to my postgresql database. I get the error:
PGError
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I tried uninstalling the gem and reinstalling, I also tried to change the paths file and put '/usr/local/bin/' on top. I tried some of the things from post:
Repairing Postgresql after upgrading to OSX 10.7 Lion
This app worked fine before updating my gems, other apps still connect just fine, to the same server. I have the same settings in my database.yml file.. what could be wrong?
The error comes from the PostgreSQL server and I have seen it many times. It tells you that you are trying to connect via Unix domain socket (and not via TCP/IP!) to a server that is running locally and listening at port 5432. But no server can be found that would accept connections like that.
You did not mention where the PostgreSQL server resides - I assume you actually mean to connect to a database server on your local machine.
Check your setup, especially your pg_hba.conf file. You need a line like:
local mydb myuser md5
or
local all all peer
or some other connection method that includes your user and database.
These would not help in your case:
host ...
or
hostssl ...
They concern TCP/IP connections, not local connections via UNIX domain socket. When you connect to localhost you actually use TCP/IP via local loop and these settings apply.
Remember to reload after you edit pg_hba.conf. I quote the manual at the linked site:
If you edit the file on an active system, you will need to signal the
postmaster (using pg_ctl reload or kill -HUP) to make it re-read the
file.

Resources