After restarting my MacBook Pro I am unable to start the database server:
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"?
I checked the logs and the following line appears over and over again:
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.0.4.
9.0.4 was the version that came preinstalled on the mac, 9.2[.4] is the version I installed via Homebrew.
As mentioned, this used to work before the restart, so it can't actually be a compiling issue. I also re-ran initdb /usr/local/var/postgres -E utf8 and the file still exists.
If you recently upgraded postgres to latest version, you can run the below command to upgrade your postgres data directory retaining all data:
brew postgresql-upgrade-database
The above command is taken from the output of brew info postgres
Note: this won't work for upgrading from 14 to 15 as of recent testing.
If you are looking for the nuclear option (delete all data and get a fresh database), you can do:
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
and then you'll need to rake db:setup and rake db:migrate from your Rails app to get setup again.
Try this :
https://gist.github.com/joho/3735740
It worked perfectly for me.
In the end it also generates you 2 bash scripts to check your DB and remove the old cluster.
Really Awesome.
see: http://www.postgresql.org/docs/9.2/static/pgupgrade.html to understand more.
Found on internet, this solution work fine for me.
When I tried to start postgresql server after upgrade to OS X 10.10 Yosemite, I encountered with a next problem:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
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"?
Okay, lets take a look into server logs:
cat /usr/local/var/postgres/server.log
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.5.
So, we need to follow a few steps after upgrade postgresql:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres92
brew update
brew upgrade postgresql
initdb /usr/local/var/postgres -E utf8
pg_upgrade -b /usr/local/Cellar/postgresql/9.2.3/bin -B /usr/local/Cellar/postgresql/9.3.5_1/bin -d /usr/local/var/postgres92 -D /usr/local/var/postgres
cp /usr/local/Cellar/postgresql/9.3.5_1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
rm -rf /usr/local/var/postgres92
That's it.
If you want to keep the previous version of postgres, use brew switch:
$ brew info postgresql
postgresql: stable 10.5 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
Conflicts with:
postgres-xc (because postgresql and postgres-xc install the same binaries.)
/usr/local/Cellar/postgresql/9.6.3 (3,259 files, 36.6MB)
Poured from bottle on 2017-07-09 at 22:15:41
/usr/local/Cellar/postgresql/10.5 (1,705 files, 20.8MB) *
Poured from bottle on 2018-11-04 at 15:13:13
$ brew switch postgresql 9.6.3
$ brew services stop postgresql
$ brew services start postgresql
Otherwise, consider this brew command to migrate existing data: brew postgresql-upgrade-database. Check out the source code.
As #Gowtham mentioned, you can solve this problem by executing a brew command
brew postgresql-upgrade-database
The above command is taken from the output of brew info postgres
However, don't forget to stop the postgres service before executing it, use the following command:
`brew services stop postgresql
you may have to start the service again.
So the final order of commands are:
brew services stop postgresql
brew postgresql-upgrade-database
brew services start postgresql
Similar to these answers (1, 2), my Postgres database files were incompatible to my Postgres version after upgrading to postgresql 13.3.
Unfortunately, upgrading my Postgres data directory failed.
$ brew postgresql-upgrade-database
...
Setting next OID for new cluster
*failure*
Consult the last few lines of "pg_upgrade_utility.log" for
the probable cause of the failure.
Failure, exiting
Error: Upgrading postgresql data from 12 to 13 failed!
==> Removing empty postgresql initdb database...
==> Moving postgresql data back from /usr/local/var/postgres.old to /usr/local/var/postgres...
Error: Failure while executing; `/usr/local/opt/postgresql/bin/pg_upgrade -r -b /usr/local/Cellar/postgresql#12/12.7/bin -B /usr/local/opt/postgresql/bin -d /usr/local/var/postgres.old -D /usr/local/var/postgres -j 8` exited with 1.
My workaround for this was to reinstall postgresql 12.7.
$ brew reinstall postgresql#12
$ brew services start postgresql#12
This can also occur when running a new Postgres in Docker, and your old volume isn't updated.
If you don't need to keep your data easiest is to clear the old volumes in the docker folder, in Linux that's here:
/var/lib/docker/volumes
It happened for me when I was trying to start Postgres12 with postgres11 mounted volume. Just deleting the mounted volume for postgres11 and restart worked for me.
Previously I was using:
docker run -d --name my_database -v /Users/champ/postgres:/var/lib/postgresql/data -p 54320:5432 postgres:11
I deleted /Users/champ/postgres and restarted postgres 12, using
docker run -d --name my_database -v /Users/champ/postgres:/var/lib/postgresql/data -p 54320:5432 postgres:12
brew info postgres will give you hints Like To migrate existing data from a previous major version of PostgreSQL run:
So in my case removing the old one rm -rf /usr/local/var/postgres.old and upgrading the DB brew postgresql-upgrade-database
A stale postmaster.pid file caused this for me.
Simply navigate to your postgres directory /Users/st/Library/Application Support/Postgres/, for me, that's:
cd '/Users/<username>/Library/Application Support/Postgres/var-10'
Then delete the file postmaster.pid. Restart postgres and it will work.
To me works with this command:
brew install --build-from-source postgresql#12
Then started Postgres:
brew services start postgresql#12
You can also check the port 5432 is listen:
netstat -nl |grep 5432
Related
I installed Postgresql 9.4.0 installed on my Mac (10.10.1/Yosemite) using homebrew. It does not work.
I have created the softlink to /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist in ~/Library/LaunchAgents.
If I try to manually load postgres I get the message that the "Operation is in progress"
> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.0/homebrew.mxcl.postgresql.plist: Operation already in progress
However postgres does not appear to be running.
> ps auxw | grep post
billmcn 670 0.0 0.0 2424272 452 s000 R+ 10:12PM 0:00.01 grep post
and I cannot connect with the command line client.
> psql
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"?
To my knowledge I have tried all the fixes suggested on other Stackoverflow threads discussing this problem. Specifically:
I have uninstalled and reinstalled postgres and the accompanying Ruby gem. There is no postgres 8.0 version on my machine.
I have verified that the psql client program is the 9.4.0 version installed by Homebrew and not a Mac system binary.
I have verified that the /usr/local/var/postgres/postmaster.pid does not exist.
I have rebooted the machine.
I did have Homebrew postgres working on this machine earlier. I think what broke it is upgrading from version 8 to version 9 but I'm not sure.
I don't have any databases I need to preserve. I'm willing to start clean with postgres; I just need to get it to work now. Any ideas?
The issue appears to have been permissions on the /usr/local/var/postgres directory. Here is what my var directory looked like when things weren't working.
ll /usr/local/var/
drwxr-xr-x 3 billmcn admin 102 Dec 20 12:44 cache
drwxr--r-- 2 root admin 68 Dec 29 21:37 postgres
(whoami = "billmcn")
I deleted /usr/local/var/postgres, uninstalled and reinstalled postgres, and now it looks like this.
ll /usr/local/var/
drwxr-xr-x 3 billmcn admin 102 Dec 20 12:44 cache
drwx------ 23 billmcn admin 782 Dec 30 10:51 postgres
Not sure how it got into this state because I don't remember futzing with the permissions on this directory, but no matter. It works now.
I had the same problem installing postgres using homebrew on a freshly installed Yosemite.
First off my brew config looks like this:
HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: 9f6926265f8e4be7cc80dfe9042f2cd3c1e8dc9e
Last commit: 64 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
CPU: quad-core 64-bit sandybridge
OS X: 10.10.1-x86_64
Xcode: 6.1.1
Clang: 6.0 build 600
X11: N/A
System Ruby: 2.0.0-481
Perl: /usr/bin/perl
Python: /usr/bin/python
Ruby: ~/.rvm/rubies/ruby-2.1.1/bin/ruby
First thing i noticed was that I had no write permission to /usr/local/var/postgres. This was easily changed issuing sudo chown -R `whoami` /usr/local/var/postgres then I reinstalled postgresql and did
cat /usr/local/var/postgres/server.log
which revealed:
postgres cannot access the server configuration file "/usr/local/var/postgres/postgresql.conf": No such file or directory
So I removed the directory /usr/local/var/postgres and issued the command to initialize the database.
initdb -D /usr/local/var/postgres/
This seemed to have done the trick and postgres is running fine.
I had this same problem. The primary issue here is that the initdb step of installation will create the directory with root ownership instead of as the user on a Mac. To solve this issue:
Create the data directory before running initdb and set permissions of 0700
rm -rf /usr/local/var/postgres # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
Then run initdb and it will respect the permissions of the data directory.
initdb -D /usr/local/var/postgres
For grins and giggles, create a test db named after your user:
createdb `whoami`
Login to test:
psql
After trying to install postgresql with Homebrew, I got this:
Warning: postgresql-9.5.2 already installed, it's just not linked
So I tried:
brew link postgresql
And got this error:
Linking /usr/local/Cellar/postgresql/9.5.2...
Error: Could not symlink share/man/man3/SPI_connect.3
/usr/local/share/man/man3 is not writable.
It seemed to be a write permission matter, so I did:
sudo chown -R `whoami` /usr/local/share/man/
It did the trick because, then I was able to do (without error):
brew link postgresql
In case anyone upgraded from a previous version, dont forget to:
brew postgresql-upgrade-database
That will solve the problem by upgrading your existing databases to the version you upgraded postgres to.
Please note that their is a thread on Homebrew's github dealing with this issue: https://github.com/Homebrew/homebrew/issues/35240
I have had a similar issue. James answer helped me solve it. But I then ran into the issue jbk is mentioning (after having deleted /usr/local/var/postgres, it kept on being recreated).
The issue is that if you have created the symlink:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
and launched the process:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
you should first unload it:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
before running James's commands.
rm -rf /usr/local/var/postgres # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
In addition, if, like me, you have an admin user managing homebrew and a regular user who will be using pgsl for development purpose, James command should be run as super user:
sudo -s
and ownership over the postgres directory should be given to your dev user:
chown my-dev-user /usr/local/var/postgres
The following command, run as the dev user, should then properly populate the directory:
createdb `whoami`
Running:
psql -l
should show you the tables and user permissions in postgre after such manipulations.
Hope this helps.
I had to delete the .pid file after seeing this in the logs
/usr/local/var/log/postgres.log
2021-10-10 19:05:27.468 BST [41868] FATAL: lock file "postmaster.pid" already exists
2021-10-10 19:05:27.468 BST [41868] HINT: Is another postmaster (PID 820) running in data directory "/usr/local/var/postgres"?
rm /usr/local/var/postgres/postmaster.pid
I installed it using brew
based on #James answer this is what I did on my M1 Monterey machine. For me the directory differed.
DANGER: In the comments it has been pointed out that my script deletes the database.
In terminal:
#to fix postgresql of existing installation
cd /opt/homebrew/var
rm -rf postgres
mkdir postgres
chmod 0700 postgres
initdb -D postgres
#install postgres
echo "installing postgres..."
brew install postgresql
brew services restart postgresql
createuser postgres -s
I then could brew install --cask pgadmin4 and run pgadmin from Applications and connect to 127.0.0.1.
I recently had a problem which began when I upgraded some brew updates / upgrades, mainly python versions etc. What worked for me.
brew uninstall postgres
brew install postgresql#9.5
echo 'export PATH="/usr/local/opt/postgresql#9.5/bin:$PATH"' > ~/.zshrc
# you may need > ~/.bashrc if you use bash
I needed pg_dump, pg_restore etc so to get that working I did
brew install libpq
Start the service
brew services start postgresql#9.5
From here I would have expected everything to work but still all rails db commands were giving error that server was not running. This final bit was the missing piece of the puzzle which finally solved it for me.
gem uninstall pg
gem install pg -v 0.20.0 # which was set in Gemfile
# could also just probably do bundle install instead.
For posterity, I had this issue and wanted to note what worked for me.
I am running postgres 11.2 on High Sierra. I had recently upgraded from postgres 10 with brew postgresql-upgrade-database.
I kept getting the error psql: could not connect to server: No such file or directory, and my server.log indicated is another postmaster (PID 5894) running in data directory "/usr/local/var/postgres"?
I tried several solutions including restarting my computer, deleting postmaster.pid, using brew services restart postgres, but to no avail. I eventually stumbled on the solution:
brew unlink postgresql && brew link postgresql
No idea why this worked, but putting it here mostly so I can reference it myself in the future! Throw stuff at the wall till it sticks!
Check #leo_chaz_maltrait for fixing errors the error Could not symlink share/man/man3/SPI_connect.3
Another error that might show up is:
Error: Could not symlink lib/pkgconfig/libecpg.pc
sudo chown -R `whoami` /usr/local/lib/pkgconfig
brew link postgresql
Please read and follow the instructions.
Check postgres logs to see what the issue is.
tail -f /usr/local/var/log/postgres.log
tail -f /opt/homebrew/var/log/postgres.log
tail -f /usr/local/var/postgres/server.log
In my case it this was the error.
2022-07-19 21:16:12.095 IST [2138] FATAL: data directory "/usr/local/var/postgres" has invalid permissions
[3472] FATAL: lock file "postmaster.pid" already exists
Added the required permission and issue got fixed.
sudo chown -R vikas /usr/local/var/postgres
rm /usr/local/var/postgres/postmaster.pid
That's it.
I'd this issue after shutting down the computer due power outage.
# This initialize your database with the current data and settings
initdb -D postgres
# This will start database service
pg_ctl -D postgres -l logfile start
Ok. I have been trying to solve this problem for several days. I installed, uninstalled, and reinstalled Postgresql 3 times. I followed precisely the instructions in this forum: https://dba.stackexchange.com/questions/42048/cant-connect-to-the-postgres-server-ls-tmp-s-pgsql-5432-no-such-file-or-dir
I found this solution in many forums, so I tried to run:
$ mkdir /var/pgsql_socket/
$ sudo mkdir /var/pgsql_socket/
$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
But this didn't work. When I try to start the server it still says there is another one running and then proceeds to fail every time I try to create a database or type "psql"
I then tried to run the following in order to change the path of the commands from OS X's builtin version of postgres to my version and it seemed to work:
$ cd /usr/local/bin
$ rm postgres
$ ln -s /Library/PostgreSQL/9.2/bin/postgres postgres
$ rm psql
$ ln -s /Library/PostgreSQL/9.2/bin/psql psql
$ rm pg_ctl
$ ln -s /Library/PostgreSQL/9.2/bin/pg_ctl pg_ctl
So then I ran the following to create a user for postgres:
$ sudo -u postgres createuser --superuser $Sarah
$ sudo -u postgres createuser --superuser user_sarah
$ sudo -u postgres psql postgres
But it kept saying "unknown user postgres"
I then tried to install the Ruby pg gem, but that also failed, saying there was a problem with necessary libraries.
I have saved a text file of everything I tried to do in the terminal. Let me know if I should post that. Thanks.
update:
When I try to run this:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
I get this:
-bash: /usr/local/bin/pg_ctl: No such file or directory
Which is different from a lot of other errors that I have seen posted on this problem.
first, you should verify that there is no postmaster running: ps -ef | grep postmaster. once you've verified the postmaster is not running, you should look into the postgresql command initd. Depending on your server installation, it may or may not be installed. You need to create a database before attempting to start postgres. It sounds to me like you got an installation that didn't install the main postgresql commands to /usr/bin, pg_ctl being one of these.
FYI: the postgres account is not a login account and is automatically created when installing postgresql, so it should be there if you had a good installation. You cannot sudo as postgres if postgres is not in the sudoers file.
The homebrew install of PostgreSQL does not create or need a postgres account, so all the mentions of doing sudo -u postgresor ps -fu postgres don't apply to your case.
The command brew info postgresql outputs various information about to start and stop it, you may read them. By contrast you don't want to put blind faith into what random users tell about how they "fixed" their non-working installation. In fact, the web carries a shocking amount of bad advice concerning PostgreSQL on MacOS X, and to me the answer you linked on dba.se is among them. It's wrong from start to finish, and you should note that it was not accepted as an answer. Certainly the author means well, but he fails to see that his own context can't be generalized to other installations.
The worst part is the suggestion to delete in /usr/local/bin/ the binaries postgres, psql, pg_ctl and soft-linking them into supposed equivalents inside /Library/PostgreSQL/9.2/bin/.
To me it just breaks the homebrew install.
No wonder that after doing this you get this error:
/usr/local/bin/pg_ctl: No such file or directory
So my answer would be to reinstall postgresql with brew to restart from a clean state, then make sure that all postgres commands you launch are from /usr/local/bin, and always first read the server log file passed to pg_ctl if you have any doubt on anything PG-related.
I am using OSX 10.8 ( looks like some version on Postgres is installed by default ) but still I did these steps one by one and they all succeeded .
brew install postgresql
initdb `brew --prefix`/var/postgres -E utf8
touch Gemfile
bundle config build.pg --with-pg-config=`brew --prefix postgresql`/bin/pg_config
And this is a screen shot of the last success message I saw:
Then I ran this one:
postgres -D `brew --prefix`/var/postgres
And that's the error I get:
postgres cannot access the server configuration file
"/usr/local/var/postgre/postgresql.conf": No such file or directory
I think your whole problem is with a short typo. Compare your error message with what you are doing. Your error message is:
postgres cannot access the server configuration file "/usr/local/ var/postgre/ postgresql.conf": No such file or directory
Your command is
postgres -D brew --prefix/var/postgres
My guess is you are accidently running
postgres -D `brew --prefix`/var/postgre
Upon restarting my Mac I got the dreaded Postgres error:
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"?
The reason this happened is because my macbook froze completely due to an unrelated issue and I had to do a hard reboot using the power button. After rebooting I couldn't start Postgres because of this error.
WARNING: If you delete postmaster.pid without making sure there are really no postgres processes running you, could permanently corrupt your database. (PostgreSQL should delete it automatically if the postmaster has exited.).
SOLUTION: This fixed the issue--I deleted this file, and then everything worked!
/usr/local/var/postgres/postmaster.pid
--
and here is how I figured out why this needed to be deleted.
I used the following command to see if there were any PG processes running. for me there were none, I couldn't even start the PG server:
ps auxw | grep post
I searched for the file .s.PGSQL.5432 that was in the error message above. i used the following command:
sudo find / -name .s.PGSQL.5432 -ls
this didn't show anything after searching my whole computer so the file didn't exist, but obviously psql "wanted it to" or "thought it was there".
I took a look at my server logs and saw the following error:
cat /usr/local/var/postgres/server.log
at the end of the server log I see the following error:
FATAL: pre-existing shared memory block (key 5432001, ID 65538) is still in use
HINT: If you're sure there are no old server processes still running, remove the shared memory block or just delete the file "postmaster.pid".
Following the advice in the error message, I deleted the postmaster.pid file in the same directory as server.log. This resolved the issue and I was able to restart.
So, it seems that my macbook freezing and being hard-rebooted caused Postgres to think that it's processes were still running even after reboot. Deleting this file resolved. Lots of people have similar issues but most the answers had to do with file permissions, whereas in my case things were different.
None of the above worked for me. I had to reinstall Postgres the following way :
Uninstall postgresql with brew : brew uninstall postgresql
brew doctor (fix whatever is here)
brew cleanup
Remove all Postgres folders :
rm -r /usr/local/var/postgres
rm -r ~/Library/Application\ Support/Postgres
Reinstall postgresql with brew : brew install postgresql
Start server : brew services start postgresql
You should now have to create your databases... (createdb)
If you're on macOS and installed postgres via homebrew, try restarting it with
brew services restart postgresql
If you're on Ubuntu, you can restart it with either one of these commands
sudo service postgresql restart
sudo /etc/init.d/postgresql restart
Maybe this is unrelated but a similar error appears when you upgrade postgres to a major version using brew; using brew info postgresql found out this that helped:
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
Here is my way:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm /usr/local/var/postgres/postmaster.pid
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
The following commands helped me out. The issue was with the PostgreSQL data version. Once upgraded, it started working fine for me.
brew upgrade postgresql
brew postgresql-upgrade-database
brew services restart postgresql
if your postmaster.pid is gone and you can't restart or anything, do this:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
as explained here initially
For me, the solution was simply restart my computer. I first tried restarting with Brew services and when that didn't work, restarting seemed like the next best option to try before looking into some of the more involved solutions. Everything worked as it should after.
Another class of reasons why this can happen is due to Postgres version updates.
You can confirm this is a problem by looking at the postgres logs:
tail -n 10000 /usr/local/var/log/postgres.log
and seeing entries like:
DETAIL: The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.
In this case (assuming you are on Mac and using brew), just run:
brew postgresql-upgrade-database
(Oddly, it failed on run 1 and worked on run 2, so try it twice before giving up)
Hello world :)The best but strange way for me was to do next things.
1) Download postgres93.app or other version. Add this app into /Applications/ folder.
2) Add a row (command) into the file .bash_profile (which is in my home directory):
export PATH=/Applications/Postgres93.app/Contents/MacOS/bin/:$PATH
It's a PATH to psql from Postgres93.app. The row (command) runs every time console is started.
3) Launch Postgres93.app from /Applications/ folder. It starts a local server (port is "5432" and host is "localhost").
4) After all of this manipulations I was glad to run $ createuser -SRDP user_name and other commands and to see that it worked! Postgres93.app can be made to run every time your system starts.
5) Also if you wanna see your databases graphically you should install PG Commander.app. It's good way to see your postgres DB as pretty data-tables
Of, course, it's helpful only for local server. I will be glad if this instructions help others who has faced with this problem.
This problema has many sources, and thus many answers. I've experienced each one of them.
1) If you have a crash of some sort, removing the /usr/local/var/postgres/postmaster.pid file is probably required as postgres may not have handled it properly. But ensure that no process is running.
2) Craig Ringer has pointed out in other posts that Apple's bundling of postgreSQL leads to pg gem installation issues Setting the PATH environment variable is a solution.
3) Another solution, is to uninstall and reinstall the gem. A brew update may be necessary as well.
If you stumble upon this post, if you can pinpoint one of the sources, you'll save time...
I was facing a similar issue here I solved this issue as below.
Actually the postgres process is dead, to see the status of postgres run the following command
sudo /etc/init.d/postgres status
It will says the process is dead`just start the process
sudo /etc/init.d/postgres start
This happened to me after my Mac (High Sierra) froze and I had to manually restart it (press and hold the power button). All I had to do to fix it was do a clean restart.
I had the same issue.
Most of the times, the problem is the fact that there's a leftover file
/usr/local/var/postgres/postmaster.pid
which works for most people, but my case was different - I tried googling this issue for last 3 hours, uninstalled postresql on OSX through brew, purged the database, nothing worked.
Finally, I noticed that I had an issue with brew that whenever I tried to install anything, it popped:
Error: Permission denied # rb_sysopen - /private/tmp/github_api_....
or something like it at the end of an install.
I simply did sudo chmod -R 777 /private/tmp and it finally works!
I'm writing this down because this might be a solution for someone else
I faced the same problem for psql (PostgreSQL) 9.6.11.
what worked for me -
remove postmaster.pid -- rm /usr/local/var/postgresql#9.6/postmaster.pid
restart postgres -- brew services restart postgresql#9.6
If postmaster.pid doesn't exist or the above process doesn't work then run --
sudo chmod 700 /usr/local/var/postgresql#9.6
For those running into this issue on M1 macs, try deleting this file and then restarting the brew service:
rm /opt/homebrew/var/postgres/postmaster.pid
My problem ended up being that I was using Gas Mask (a hosts file manager for Mac), and I didn't have an entry for localhost in the hosts file I was using.
I added:
127.0.0.1 localhost
And that resolved my problem.
I'm not entirely sure why, but my Postgres installation got a little bit screwed and some files were deleted resulting in the error OP is showing.
Despite the fact that I am able to run commands like brew service retart postgres and see the proper messages, this error persisted.
I went through the postgres documentation and found that my file /usr/local/var/postgres was totally empty. So I ran the following:
initdb /usr/local/var/postgres
It seems some configurations took place with that command.
Then it asked me to run this:
postgres -D /usr/local/var/postgres
And that told me a postmaster.pid file already exists.
I just needed to know if brew would be able to pick up the configs I just ran, so I tested it out.
ls /usr/local/var/postgres
That showed me a postmaster.pid file. I then did brew services stop postgresql, and the postmaster.pid file disappeared. Then I did brew services start postgresql, and VIOLA, the file reappeared.
Then I went ahead and ran my app, which did in fact find the server, however my databases seem to be gone.
Although I know that they may not be gone at all - the new initialization I did may have created a new data_area, and the old one isn't being pointed to. I'd have to look at where that's at and point it back over or just create my databases again.
Hope this helps! Reading the postgres docs helped me a lot. I hate reading answers that are like "Paste this in it works!" because I don't know what the hell is happening and why.
I had the same issue and it was due to an incompatible version after upgrading from version 11 to 13.2
Checking error log at:
/usr/local/var/log/postgres.log
Showed me:
DETAIL: The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 13.2.
To fix I ran:
brew postgresql-upgrade-database
And then started postresql with brew:
brew services start postgresql
The causes of this error are many so first locate your log file and check it for clues. It might be at /usr/local/var/log/postgres.log or /usr/local/var/postgres/server.log or possibly elsewhere. If you installed with Homebrew you can find the location in ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist.
I had this same concern when connecting trying to start a PostgreSQL database server on MacOS Monterey.
When I run the command below to restart the PostgreSQL database server:
brew services restart PostgreSQL
It restarts but when I try to check the status of the PostgreSQL database server using the command below, I get an error:
Name Status User File
mysql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
postgresql error 256 root ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Here's what worked for me:
First, I checked the log file for the PostgreSQL database server to what was the cause of the error using the command below:
cat /usr/local/var/log/postgres.log
OR
nano /usr/local/var/log/postgres.log
The logs showed the following errors:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
2022-01-25 19:01:06.051 WAT [29053] FATAL: database files are incompatible with server
2022-01-25 19:01:06.051 WAT [29053] DETAIL: The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.1.
For the root execution error I had to run the following command to fix file permissions that when messed us when I ran brew services with the sudo command prefix. Replace your-username with your MacOS username (in my case my username was promisepreston:
# Stop postgresql
sudo brew services stop PostgreSQL
# In case service file is copied to ~/Library/LaunchAgents as well
brew services stop postgresql
# Fix permission of homebrew files
sudo chown -R your-username:admin $(brew --prefix)/*
For the database files are incompatible with server I had to simply upgrade the existing PostgreSQL data files which were created using version 13 to the latest PostgreSQL version on my computer which was 14.1 by running the following command below:
brew postgresql-upgrade-database
Afterwhich, I restarted the PostgreSQL database server:
brew services restart PostgreSQL
And then checked the status using the command below:
brew services list
Then I got the output below showing that everything was working fine:
Name Status User File
mysql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
postgresql started promisepreston ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
References: Brew PostgreSQL Starts But Process Is Not Running
I had a similar error.
All of this is done from the command line (no sudo calls at all)
I verified I had PostgreSQL installed psql -V (note that's a capital "V")
I attempted to connect to the server: psql postgres
THIS IS WHERE I EXPERIENCED THE ERROR OF THIS STACK OVERFLOW QUESTION
After doing some research about possible fixes, I obviously had PostgreSQL installed, but I didn't have a default server in place.
What I had to do was create a Custom Data Directory
As far as I can tell, creating the custom data directory is the same as having a default server in place.
Since this is a new machine (MacBook Pro 2021 using apple m1 chip), I wanted to find the easiest solution possible, and I believe this Custom Data Directory is just that. The remaining steps to fix this issue are as follows:
From the home directory, I created an empty directory mkdir myData
From the home directory, Initialized a server: initdb myData (throws a bunch of files into the myData directory)
pg_ctl -D myData -l logfile start (starts the server)
psql postgres (connects to the server)
So, as someone fairly new to PostgreSQL and databases and SQL in general, couple notes:
It is possible to "quit" the connection to the server, using \q (while connected to the server, it's also possible to type "help")
It is also possible to "stop" the server, as well with pg_ctl -D myData stop
At this point I now am certain I have PostgreSQL installed, have a server I can start and stop, and have the ability to connect to/disconnect from, that server.
Go to /var/log/
and run cat postgres.log
Here you will find the reason for the failure of postgres.
If it is a smart shut down then probably your icu4c version (C++ library for Unicode) is not proper which is linked with postgres. So run the following commands.
brew upgrade
brew cleanup
This should work ;)
I've had to look up this post several times to solve this issue. There's another fix, which will also applies to similar issues with other running programs.
I just discovered you can use the following two commands.
$top
This will show all the currently running actions with their pid numbers. When you find postgres and the associated pid
$kill pid_number
I just got the same issue as I have put my machine(ubuntu) for update and got below 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"?
After completing the updating process when I restart my system error gone. And its work like charm as before.. I guess this was happened as pg was updating and another process started.
SUPER NEWBIE ALERT: I'm just learning web development and the particular tutorial I was following mentioned I have to install Postgres but didn't actually mention I have to run it as well... Once I opened the Postgres application everything was fine and dandy.
#Jagdish Barabari's answer gave me the clue I needed to resolve this. Turns out there were two versions of postgresql installed while only one was running. Purging all postgresql files and reinstalling the latest version resolved this issue for me.
I removed /usr/lib from the LD_LIBRARY_PATH and it worked.
I was working in dockerfile postgres:alpine.
This answer worked for me: https://stackoverflow.com/a/45454567/15067545 on my ubuntu system.
Command: sudo service postgresql restart.
Final update:
I had forgotten to run the initdb command.
By running this command
ps auxwww | grep postgres
I see that postgres is not running
> ps auxwww | grep postgres
remcat 1789 0.0 0.0 2434892 480 s000 R+ 11:28PM 0:00.00 grep postgres
This raises the question:
How do I start the PostgreSQL server?
Update:
> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
sh: /usr/local/var/postgres/server.log: No such file or directory
Update 2:
The touch was not successful, so I did this instead:
> mkdir /usr/local/var/postgres
> vi /usr/local/var/postgres/server.log
> ls /usr/local/var/postgres/
server.log
But when I try to start the Ruby on Rails server, I still see this:
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
Update 3:
> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running
Update 4:
I found that there wasn't any pg_hba.conf file (only file pg_hba.conf.sample), so I modified the sample and renamed it (to remover the .sample). Here are the contents:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
But I don't understand this:
> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running
Also:
sudo find / -name postgresql.conf
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
Update 5:
sudo pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
Update 6:
This seems odd:
> egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
egrep: /usr/local/var/postgres/postgresql.conf: No such file or directory
Though, I did do this:
>sudo find / -name "*postgresql.conf*"
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
/usr/share/postgresql/postgresql.conf.sample
So I did this:
egrep 'listen|port' /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
#listen_addresses = 'localhost' # what IP address(es) to listen on;
#port = 5432 # (change requires restart)
# supported by the operating system:
# %r = remote host and port
So I tried this:
> cp /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf
> cp /usr/share/postgresql/postgresql.conf.sample /usr/share/postgresql/postgresql.conf
I am still getting the same "Is the server running?" message.
The Homebrew package manager includes launchctl plists to start automatically. For more information, run brew info postgres.
Start manually
pg_ctl -D /usr/local/var/postgres start
Stop manually
pg_ctl -D /usr/local/var/postgres stop
Start automatically
"To have launchd start postgresql now and restart at login:"
brew services start postgresql
What is the result of pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start?
What is the result of pg_ctl -D /usr/local/var/postgres status?
Are there any error messages in the server.log?
Make sure tcp localhost connections are enabled in pg_hba.conf:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
Check the listen_addresses and port in postgresql.conf:
egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
#listen_addresses = 'localhost' # What IP address(es) to listen on;
#port = 5432 # (change requires restart)
Cleaning up
PostgreSQL was most likely installed via Homebrew, Fink, MacPorts or the EnterpriseDB installer.
Check the output of the following commands to determine which package manager it was installed with:
brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres
If you want to manually start and stop PostgreSQL (installed via Homebrew), the easiest way is:
brew services start postgresql
and
brew services stop postgresql
If you have a specific version, make sure to suffix the version. For example:
brew services start postgresql#10
I had almost the exact same issue, and you cited the initdb command as being the fix. This was also the solution for me, but I didn't see that anyone posted it here, so for those who are looking for it:
initdb /usr/local/var/postgres -E utf8
If your computer was abruptly restarted
You may want to start PG server but it was not.
First, you have to delete the file /usr/local/var/postgres/postmaster.pid Then you can restart the service using one of the many other mentioned methods depending on your install.
You can verify this by looking at the logs of Postgres to see what might be going on: tail -f /usr/local/var/postgres/server.log
For specific version:-
tail -f /usr/local/var/postgres#[VERSION_NUM]/server.log
Eg:
tail -f /usr/local/var/postgres#11/server.log
Another approach is using the lunchy gem (a wrapper for launchctl):
brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy
To start PostgreSQL:
lunchy start postgres
To stop PostgreSQL:
lunchy stop postgres
For further information, refer to: "How to Install PostgreSQL on a Mac With Homebrew and Lunchy"
Here my two cents: I made an alias for postgres pg_ctl and put it in file .bash_profile (my PostgreSQL version is 9.2.4, and the database path is /Library/PostgreSQL/9.2/data).
alias postgres.server="sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.2/data"
Launch a new terminal.
And then? You can start/stop your PostgreSQL server with this:
postgres.server start
postgres.server stop
The cleanest way by far to start/stop/restart PostgreSQL if you have installed it through brew is to simply unload and/or load the launchd configuration file that comes with the installation:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
The first line will stop PostgreSQL and the second line will start it. There isn't any need to specify any data directories, etc. since everything is in that file.
To start the PostgreSQL server:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
To end the PostgreSQL server:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
You can also create an alias via CLI to make it easier:
alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'
With these you can just type "pg-start" to start PostgreSQL and "pg-stop" to shut it down.
For test purposes, I think PostgreSQL App is the best option!
Run an app, and the server is up and running.
Close the app, and the server goes down.
http://postgresapp.com/
If you have installed using Homebrew, the below command should be enough.
brew services restart postgresql
This sometimes might not work. In that case, the below two commands should definitely work:
rm /usr/local/var/postgres/postmaster.pid
pg_ctl -D /usr/local/var/postgres start
# Remove old database files (if there was any)
$ rm -rf /usr/local/var/postgres
# Install the binary
$ brew install postgresql
# init it
$ initdb /usr/local/var/postgres
# Start the PostgreSQL server
$ postgres -D /usr/local/var/postgres
# Create your database
$ createdb mydb
# Access the database
$ psql mydb
psql (9.0.1)
Type "help" for help.
Sometimes it's just the version which you are missing, and you are scratching your head unnecessarily.
If you are using a specific version of PostgreSQL, for example, PostgreSQL 10, then simply do
brew services start postgresql#10
brew services stop postgresql#10
The normal brew services start postgresql won't work without a version if you have installed it for a specific version from Homebrew.
When you install PostgreSQL using Homebrew,
brew install postgres
at the end of the output, you will see this methods to start the server:
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
I think this is the best way.
You can add an alias into your .profile file for convenience.
I had the same problem and performed all updates from the first post. But after checking the log file,
/usr/local/var/postgres/server.log
I see the true cause:
FATAL: data directory "/usr/local/var/postgres" has group or world access
DETAIL: Permissions should be u=rwx (0700).
After changing permissions on this directory,
chmod 0700 /usr/local/var/postgres
the PostgreSQL server started.
Check the log file every time.
For a quick disposable test database, you can run the server in the foreground.
Initialize a new PostgreSQL database in a new directory:
mkdir db
initdb db -E utf8
createdb public
Start the server in the foreground (Ctrl + C to stop the server):
postgres -d db
In another shell session, connect to the server
psql -d public
If you didn't install it with Homebrew and directly from the Mac package, this worked for me for PostgreSQL 12 when using all the default locations, variables, etc.
$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/12/bin/pg_ctl -D /Library/PostgreSQL/12/data/ stop
Variation on this answer: https://stackoverflow.com/a/13103603/2394728
initdb `brew --prefix`/var/postgres/data -E utf8`` && pg_ctl -D /usr/local/var/postgres/data -l logfile start
PostgreSQL is integrated in Server.app available through the App Store in Mac OS X v10.8 (Mountain Lion). That means that it is already configured, and you only need to launch it, and then create users and databases.
Tip: Do not start with defining $PGDATA and so on. Take file locations as is.
You would have this file:
/Library/Server/PostgreSQL/Config/org.postgresql.postgres.plist
To start:
sudo serveradmin start postgres
Process started with arguments:
/Applications/Server.app/Contents/ServerRoot/usr/bin/postgres_real -D /Library/Server/PostgreSQL/Data -c listen_addresses=127.0.0.1,::1 -c log_connections=on -c log_directory=/Library/Logs/PostgreSQL -c log_filename=PostgreSQL.log -c log_line_prefix=%t -c log_lock_waits=on -c log_statement=ddl -c logging_collector=on -c unix_socket_directory=/private/var/pgsql_socket -c unix_socket_group=_postgres -c unix_socket_permissions=0770
You can sudo:
sudo -u _postgres psql template1
Or connect:
psql -h localhost -U _postgres postgres
You can find the data directory, version, running status and so forth with
sudo serveradmin fullstatus postgres
For development purposes, one of the simplest ways is to install Postgres.app from the official site. It can be started/stopped from Applications folder or using the following commands in terminal:
# Start
open -a Postgres
# Stop
killall Postgres
killall postgres
This worked for me (macOS v10.13 (High Sierra)):
sudo -u postgres /Library/PostgreSQL/9.6/bin/pg_ctl start -D /Library/PostgreSQL/9.6/data
Or first
cd /Library/PostgreSQL/9.6/bin/
If you installed PostgreSQL using the EnterpriseDB installer, then what Kenial suggested is the way to go:
sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data start
sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data stop
Homebrew is the way!!
To start the service:
brew services start postgresql
To list it:
brew services list | grep postgres
To stop the service:
brew services stop postgresql
If you didn't install the Postgres server with Homebrew or installed using .dmg file, try this:
$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/13/bin/pg_ctl -D /Library/PostgreSQL/13/data/ stop
For MacPorts, just use the load/unload command and the port name of the running server:
sudo port load postgresql96-server
- or -
sudo port unload postgresql96-server
so you don't have to remember where the /Library/LaunchDaemons/org.macports.postgresql96.plist file is located.
having installed Postgres with homebrew that is what I do to start postgres and keep it in foreground to see the logs:
/opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres
install postgresql using brew: brew install postgresql, you can specify the version using "#" sign: brew install postgresql#14
start postgresql: brew services start postgresql or specific version brew services start postgresql#14
stop postgresql: brew services stop postgresql
$ brew upgrade postgres
fixed it for me.
That, of course, will upgrade your PostgreSQL version and update/install any dependencies.
Warning: Do this knowing that your PostgreSQL version will likely change. For me, that wasn't a big deal.
None of the previous answers fixed the issue for me, despite getting the same error messages.
I was able to get my instance back up and running by deleting the existing postmaster.pid file which was locked and was not allowing connections.
This worked for me every time, inspired by Craig Ringer:
brew install proctools
sudo pkill -u postgres
proctools includes pkill. If you don't have Homebrew: https://brew.sh/
After doing brew services restart postgresql.
It works best to:
brew services stop postgresql
brew postgresql-upgrade-database
brew services start postgresql
Then type: psql
it now runs this was after the 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 "/tmp/.s.PGSQL.5432"?
The upgrade may be optional depending on the other dependencies your running.
Which means that rather than Restart using brew for in on mac os, Stop completely postgres and then start postgres and connect to your psql databaseName.
Hope this was useful.