"database does not exist" after pg backup - ruby-on-rails

I hope you re all doing great !
I got surprised today when I "rails s" my app : ActiveRecord::NoDatabaseError
connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "DDH_development" does not exist
The last thing I did yesterday was trying to backup my database on PGadmin (I don't rememebber how it ended actually) and when I try to connect to PGadmin :
Connection to the server at "localhost" (::1), port 5432 failed: FATAL: role "postgres" does not exist
What did I do ? I don't know where to start looking...
A few days ago, I tried to upgrade pg from 13 to 14, could it be that ?
Thanks a lot for your answers !

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.

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

Rails 4 + Postgres: Randomly cannot connect to development server

I am running Rails 4.2.2 and my postgres gem is 0.17.1 and everything was running fine yesterday but today when I opened the console and tried connecting to the database I saw 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 "/tmp/.s.PGSQL.5432"?
...then when I tried connecting to the database through running psql, I see the following:
dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /usr/local/bin/psql
Reason: image not found
Abort trap: 6
As far I know I havent changed anythingsince yesterday so I'm not sure why this is happenning. I tried adding host: localhost to my database.yml with no luck. It may be worth noting when I try to connect to the DB using a program like PSequel, I see the following:
could not connect to server: Connection refused
is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
Has anyone seen this?
Neara's response to this post provided an answer for me: PostgreSQL server won't stop.
Basically had to run:
$ brew services list
$ brew services restart postgresql
They posted a workaround in case services cannot be found.

Receiving "ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() could not receive data from server: Connection timed out" in rake task

In my rake task, I'm connecting to postgresql via a pgbouncer setup. For some queries I receive this error:
ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() could not receive data from server: Connection timed out
In my pgbouncer log, I see this entry:
closing because: client unexpected eof (age=3110)
I have disabled prepared statements in my config/database.yml file and enabled transaction pool_mode in my pgbouncer config file.
When I directly connect to postgres, I don't get this error.
Also in my rake task, I am creating multiple threads. I tried running ActiveRecord::Base.connection.reconnect! inside my threads as suggested here. That didn't help either.
I am using rails version 4.2.5 which has this fix. I'm still getting this error "could not receive data from server: Connection timed out".
This was caused due to network issues in clients. Not able to reproduce since we migrated both server and client to Google Cloud Platform. Earlier our server was in DigitalOcean while our clients were in GCP.

Creating Users on Postgres - Authentication Issues - Installing

I'm attempting to create a Ruby on Rails web app that uses postgres as its database. I am having trouble connecting to the server with postgres
I have installed PostgreSQL and have successfully created a Rails app that uses postgres. However, when I run "rails server", it returns..
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on XXX:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:1215:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I believe I have an authentication problem. I've tried to follow two SO links, which appear to have worked for a great number of people, but for which the first steps have failed for me.
How to configure postgresql for the first time?
The first step of this link (su root) resulted in: "su: Authentication Failure".
I am trying to use this guide:
https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-12-04
If I use "sudo -i", the "su - postgres" command works.
"createuser" then results in:
Enter name of role to add: XXX(root username)
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
createuser: could not connect to database postgres: 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"?
When trying to follow the Ubuntu Postgres guide at https://help.ubuntu.com/community/PostgreSQL I had less success, with
sudo -u postgres psql postgres
returning:
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"?
Thanks! I'm still a beginner, so please explain things as simply as possible and elaborate.
You still need to start the postgres server. In ubuntu sudo /etc/init.d/postgresql-8.4 restart

Resources