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

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

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 heroku - when writing heroku update - ruby on rails

i am trying to connect heroku
i downloaded heroku toolbelt and installed it .
then from the command prompt i type :
heriku login
althogh i type the right credentials (i checked!!) i get
! Unable to connect to Heroku API, please check internet connectivity and
try again.
I wanted to be sure its not something with authentication
so i typed :
heroku update
just to set up a connection that doesn't need a password.
i get the message:
! Unable to connect to Heroku API, please check internet connectivity and
try again.
i saw some answers that talked about the remote option so i did that:
heroku git:remote -a my-app-name
and i got the same response.
the internet connection is ok because the git program is able to push to github.
even when i run
heroku install:something
it works.
so I tried to debug:
i downloaded a git bash so i can operate in unix through SSH
and I followed this stack question comments and tried it:
ssh git#heroku.com -T
and got :
warning permanently added the RSA host key for ip address '50.19.85.132' to the list of
known hosts.
permission denied (publickey)
then I tried to check DNS response:
ssh host heroku.com
and got :
ssh: host: no address associated with the name.
so i guess that my connection get blocked regardless of SSH .
i run netstat -a and it seems that port: 22 , 443 , 5000 that heroku uses are free.
i turned the firewall off and still no conection
what should i do?
Try heroku login first. It authenticates you.

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.

java.sql.SQLException: FATAL: no pg_hba.conf entry for host

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).

Resources