Postgres/Mac - 'service already loaded', but 'could not connect to server' - postgresql

I'm running Homebrew-installed Postgres version 9.6.3 on my Mac (High Sierra, 10.13.3), and this morning I'm finding that Postgres is having some issues. It was working fine last night, then I put the computer to sleep... when I woke it up this morning and tried to run a Phoenix app, I got
[error] Postgrex.Protocol (#PID<0.306.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
Running psql returned
psql: 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"?
so it seemed that perhaps the server had stopped... however, running my alias pg-start, which translates to launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist returns
/usr/local/Cellar/postgresql/9.6.3/homebrew.mxcl.postgresql.plist: service already loaded
So this is confusing, because it seems that one command suggests that Postgres is not running, while the other suggests that it is.
I can't recall for sure, but I may have stopped the server before putting the computer to sleep last night, which I actually usually do not do... my pg-stop alias is launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
though I can't see why that would cause problems, it's the only thing that sticks out in my mind as something 'different' that I may have done.
I've tried restarting my machine, but the problem persists. I'm not terribly experienced with debugging this sort of issue, so any guidance or suggestions would be much appreciated.

Well, I resolved it, though I'm not sure what the exact problem was. To get some more error info, I ran
postgres -D /usr/local/var/postgres
which gave me
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 323) running in data directory "/usr/local/var/postgres"?
which I'd encountered before, so I kind of knew how to proceed...
Here are the steps I took to resolve this:
First, I ran
pg_ctl -D /usr/local/var/postgres start
which returned
pg_ctl: another server might be running; trying to start server anyway
server starting
My-MBP:~ me$ FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 1188) running in data directory "/usr/local/var/postgres"?
then I ran my alias pg-stop
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
followed by
pg_ctl -D /usr/local/var/postgres start
again. This time, it returned
server starting
My-MBP:~ me$ LOG: database system was shut down at 2018-02-07 11:10:43 EST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
and now Postgres was running correctly - psql commands, etc.
However, now my alias pg-stop wouldn't work -
/Users/me/Library/LaunchAgents/homebrew.mxcl.postgresql.plist: Could not find specified service
I ran
pg_ctl -D /usr/local/var/postgres stop
and then my pg-stop alias was restored. So now pg-start and pg-stop are working as they should be.
I hope that this is helpful to someone in the future, but if anyone can explain what happened here, I'd really appreciated having a deeper understanding of what went wrong.

Related

Postgres + Intellj: Fatal role can't connect to database

Try to connect to the database through Intellj for Postgres. I just keep getting the error message. Not sure if I can include any specific details, because this is connect through IntelliJ so there's not a lot of details I can tell. The progress is running with docker and ./gradlew sDS to kick off the running volume.
The specified database user/password combination is rejected: [28000] FATAL: role "username" does not exist.
I got it to resolved, I'm having another Postgres running on the computer. Just want to mention anyone who have the same trouble as me, make sure to check if your port is not running when you run this command. If after you shut down everything with docker but they'll a port still running then you need to track down and stop other Postgres that is currently running.
lsof -nP -iTCP -sTCP:LISTEN | grep "port #"

PostgresSQL is not getting connected

I am new to Ubuntu operating system and trying to install postgresSQL in my system.
After completing the installation and entering postgres mode when I enter the command pgsql, it gives me this error
psql: 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"?
what could possibly be wrong??
Probably the postgresql server is not running; you can run netstat -adp to see if postgres is listening on port 5432.
If it isn't, then it most likely isn't running; systemctl status postgresql will help identify if it is even installed, and if it has run into any issues (for further details, see https://mydbanotebook.org/post/troubleshooting-01/)
If it is running and seems healthy, then it might not be configured to accept connections in whichever way you are connecting (which user, database, host/network etc) - see details about the pg_hba.conf file that includes rules for this - https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
After install, you must create an instance with initdb.
Later, change the listen_address to *, restart the service and now you can check the port.
Check the pg log for the message: "postgres is ready to accept connections"

Mystery: All postgres databases disapeared

I'm developing a Rails application with a postgresql database.
MacOS 10.15.3
ruby 2.6.3p62
postgres 12.3
I took a break and when I came back my laptop was slow to wake up. After pressing the on/off fingerprint button a few times, it eventually went into the startup sequence. This was odd because I did not recall shutting it off.
After starting, everything seemed normal. I opened some applications and resumed my work. But, when I tried to run a database migration, I got errors about not being able to connect to the server.
$ bundle exec rake db:migrate
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.5-compliant syntax, but you are running 2.6.3.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
rake aborted!
PG::ConnectionBad: 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?
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I tried to start it with homebrew but no luck. I eventually read another question that suggested installing the Postgres app and using this to force it to restart. At first the app said "Empty data directory". I clicked "Initialize" and it worked. And I no longer get the bad connection error. But ALL my previous postgres databases are gone.
I'm puzzled. How could this have happened? And with almost no interaction from me whatsoever! Did Postgres.app wipe all my previous data with one click?

Postgres service started, but can't connect. Nothing listening on port 5432

I don't know how this happened, but my postgres server (9.6.2) has stopped working locally. I seem to be able to start it up, and stop it using:
brew services start postgresql and brew services start postgresql but when I try to connect using psql I get this error:
psql: 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"?
When I try to see if anything is listening on port 5432 using lsof -i :5432 I get nothing.
Looked at other posts that say check the pg_hba.conf file, which I did, but doesn't seem to be the problem.
I ended up reinstalling postgres via homebrew. That by itself didn't do enough. I had to kill my data directory to get this to work (i.e. rm -rf /path/to/my/data/directory) and then rebuild it using initdb.
This solution obviously only works if you don't care about keeping your data (in my case, it was just data for my dev environment, so I didn't). If you do want to save a copy of your data, I you'd have to come up with something else.

Postgres Database Local Installation / Running

Trying to setup Postgres / PostGIS on OSX Mountain Lion to use with Django. However, something is wrong and I am not sure how to fix it.
$ createdb geo
createdb: 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/pgsql_socket/.s.PGSQL.5432"?
$ postgres -D /usr/local/var/postgres
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 221) running in data directory "/usr/local/var/postgres"?
$ pg_ctl -D /usr/local/var/postgres -l logfile start
pg_ctl: another server might be running; trying to start server anyway
server starting
Then I found this gist that said how to fix it on lion. However it did not work for me:
$ kill -9 221
$ pg_ctl -D /usr/local/var/postgres stop
waiting for server to shut down.... done
server stopped
$ pg_ctl -D /usr/local/var/postgres start
pg_ctl: another server might be running; trying to start server anyway
server starting
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 1436) running in data directory "/usr/local/var/postgres"?
Another attempt:
$ cd /usr/local/var/postgres
$ mv postmaster.pid postmaster.backup
$ pg_ctl -D /usr/local/var/postgres start
server starting
$ LOG: could not bind IPv6 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: could not bind IPv6 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
Has anyone successfully done this with mountain lion? If so, what am I missing? Thanks for your help with this issue!
This message:
createdb: 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/pgsql_socket/.s.PGSQL.5432"?
indicates that your PostgreSQL server doesn't use /var/pgsql_socket as its Unix domain socket directory, which is the rendez-vous point between the server and the client. This is quite common, since only PostgreSQL shipped by Apple with OSX server uses that path. Incidentally, this is why the createdb command as shipped by Apple tries to connect to the server as configured by Apple.
From that point, there are a lot of different solutions to work around the problem, but the more obvious one would be to use the createdb command that comes with your PostgreSQL installation instead of the one that is preinstalled in your system.
Should you need further help with that, you should specify how you installed PostgreSQL, because there are quite a few different packages that provide it, and they all use different paths to avoid trampling over each other.
Update for Homebrew
If using brew, I suggest to apply the answer on superuser.com:
How should I set the PATH variable on my Mac so the Hombrew-installed tools are found?
Presumably the author of the Postgres+Postgis tutorial you're referring to has changed its PATH long ago and forgot to mention it in his text.
Once done, the command which createdb should return /usr/local/bin/psql which is the homebrew version instead of the initial /usr/bin/psql which is the Apple version.