Repairing Postgresql after upgrading to OSX 10.7.3 Lion - ruby

I have the issue described in:
Repairing Postgresql after upgrading to OSX 10.7 Lion
but didn't get it solved.
The Postgres application generating the error is at /usr/bin/psql,
but the correct one is at
/Library/PostgreSQL/9.1/bin/psql.
When I add the later to my path in .bash_profile (or even in /etc/paths), uninstall and install the PG gem, then I still receive the error:
.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `initialize': could not connect to server: Permission denied (PG::Error) Is the server running locally and accepting
I tried it several times, also with the option:
-- --with-pg-config=/Library/PostgreSQL/9.1/bin/pg_config
Any idea?
EDIT:
After reading through more posts (like http://www.iainlbc.com/2011/10/osx-lion-postgres-could-not-connect-to-database-postgres-after-homebrew-installation),
I just did the following:
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
And it worked. I don't know at all why. Do I have two installations of Postgres? Where is the app laying behind the /usr/bin/psql file?

After running find / -name .s.PGSQL.5432 -ls, I noticed that the result was:
47366187 0 srwxrwxrwx 1 postgres wheel 0 Dec 3 15:15 /private/tmp/.s.PGSQL.5432
Note that the PGSQL socket file is /private/tmp/.s.PGSQL.5432
Using your suggestion above (but modifying it a bit), I did this:
sudo ln -s /private/tmp /var/pgsql_socket
Restarted terminal and it worked.

Related

/etc/sudoers is owned by uid 503, should be 0 - El Capitan

Whenever I try any Sudo commands in terminal, I get the following error message:
sudo: /etc/sudoers is owned by uid 503, should be 0
sudo: no valid sudoers sources found, quitting
I have tried logging into single user mode and typing in the following commands:
mount -uw
chown /private/etc/sudoers 0
After typing in the chown ownership line, I get an error message saying
“illegal username”.
Other things I have tried:
Reinstalling the os (El Capitan)
Disabling System Integrity Protection (SIP).
Calling Apple Care who say that Sudo commands have been disabled in El Capitan.
Logging in under Single User mode and typing in the following code:
chown root:wheel /private/etc/sudoers
This produced the error message “Operation Not Permitted” in my Standard, Admin, and Root accounts.
The error message “Read-Only File System” came up when I logged tried the code in Single User Mode.
FYI
When I run ls -la /private/etc/sudoers in Terminal, I get the following:
-rw-r-----# 1 MY-ADMIN-USERNAME staff 67 18 Feb 14:03 /private/etc/sudoers
Note I’ve replaced my actual admin username with “MY-ADMIN-USERNAME” just so you know what’s showing.
I need Sudo commands to work for a range of reasons, one of which is to get CrashPlan to work.
My hardware is a 2010 iMac, 3.2GHZ, 16GB Ram and 500GB SSD which was installed about a year ago.
These problems have only come up with El Capitan. I didn’t have them in the past with Yosemite. Downgrading is likely to be a problem as Migration Assistant gives problems when trying to port the data across by telling you that Time Machine is working off a later version of OsX.
Looking around online, I can see that many people have had similar issues but the resolutions that have worked for them unfortunately have not worked for me.
Any suggestions would be greatly appreciated.
The correct chown syntax is
chown 0 /private/etc/sudoers
try running the command from another operating system, so for example from a mac os x 10.9, or 10.10 partition:
sudo chown 0 /Volumes/NAME_OF_VOLUME/private/etc/sudoers
This is what worked for me. you might also be able to do this from a 10.9, or 10.10 recovery usb environment.

Homebrew postgres broken

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

psql: could not connect to server: No such file or directory (Mac OS X)

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.

Postgres.app Could not start on port 5432

I'm using http://postgresapp.com. In the menubar it gives the error " Could not start on port 5432." Similarly if I try to start the server from the terminal, I get:
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"?
I also ran pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
and got the output server starting but still get the same errors when connecting to psql.
If you are running your server on a macOS machine and installed Postgres through Homebrew, you can stop the current instance like this:
brew services stop postgresql
Then click the Elephant in the native menu-bar at the top of the screen and it should successfully startup.
You can stop the process by finding the PID with
lsof -i :5432
and then killing it with
kill -9 <PID>
If you've installed Postgres via another method (for example, from www.postgresql.org) and it's starting automatically at startup, you can prevent that Postgres from starting via the following:
sudo launchctl unload /Library/LaunchDaemons/com.edb.launchd.postgresql-X.X.plist
sudo rm -f /Library/LaunchDaemons/com.edb.launchd.postgresql-X.X.plist
TIP: use tab to autocomplete after the com.edb.launchd part to figure out what version is loading.
Restart Postgres.app and you should be good to go.
(from http://forums.enterprisedb.com/posts/list/1831.page;jsessionid=70621DC48C99EDE663A6A594B05F1A02#6782)
I was just having this exact issue. When I ran which psql it was pointing at the Postgres client tools installed with Lion:
/usr/bin/psql
Using a hint from Frank Wiles I ran ps auxw | grep post to confirm that postgres was running and that it was running on the right port, that also showed me the postgres.app path:
/Applications/Postgres.app/Contents/MacOS/bin/postgres.
So I edited by .bash_profile to export that directory. On first effort I added it to the end of the path. When I ran echo $PATH I could see that usr/bin was the first thing in the path, and which psql still gave the /usr/bin path. At that point a friend guided me in the right direction:
export PATH="/Applications/Postgres.app/Contents/MacOS/bin:${PATH}"
Start a new terminal window, then run which psql -- it should point to the postgres.app location and psql should fire up the postgres shell. Works fine now.
I have tried just about every solution to this problem that is out there. For me it always happens when my MacBook's battery dies, even if the computer is already sleeping. I was poking around in ~/Library/Application\ Support/Postgres/var-9.4 and I discovered another postmaster.pid file that I had not seen before. I deleted it, and now everything is back up and running! I am running the Postgres.app version, not the brew version.
Steps I took:
Make sure postgres.app is not running.
Run rm ~/Library/Application\ Support/Postgres/var-9.$X/postmaster.pid
If you don't have a var-9.$X directory, just run rm ~/Library/Application\ Support/Postgres/postmaster.pid
Restart postgres.app
Get back to developing cool stuff.
This command is a one-liner that instantly kills all PostgresSQL processes.
sudo kill -kill $(sudo lsof -t -i :5432)
This fixed all my problems on Mac OSX Mojave 10.14.1.
You my have another instance of postgres running, that is the only application interested in this port. You should use netstat and ps to determine this. Then stop the instance and uninstall it, you mayhave installed enterpiseDb for example, that is what I did.
I generally face this issue on my mac, and this fixes it for me always
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
Hope this is helpful
I had a similar problem where I could not connect to the Postgres.app even though the app itself said that it is running on port 5432.
I am not sure why, but even when I quit the app and checked that no postgres processes was running with ps -a. these files existed:
/tmp/.s.PGSQL.5432
and
/tmp/.s.PGSQL.5432.lock
My solution was to delete these files and then start the postgres.app again.
This is what worked for me:
$ sudo pkill -u postgres
Props to this resource:
https://github.com/PostgresApp/PostgresApp/issues/197#issuecomment-474534056
It can also happen that the PID is taken.
This ocurred to me when the Computer suffered an unexpected reboot.
If so, you must go to:
˜/Library/Application Support/Postgres/var-9.4
You can check that path in Postgres.app Preferences dialog.
And then, just remove the pid file
sudo rm postmaster.pid
And the server starts up right away.
When trying to open the Postgres app was getting that same error regarding post 5432 (on Mac OSX 10.10.5)
I did:
$ lsof -i | grep LISTEN
Saw which PID was running postgres at that port, did:
$ killall {pid} and then $ brew uninstall postgres
After that, restarted my Mac, and ran:
$ lsof -i | grep LISTEN again just to make sure. Saw no postgres running anywhere and was able to open the Postgres app without getting that warning.
Then I reinstalled postgres with $ brew install postgres
ANOTHER OPTION:
(I also tried this one time when the above approach didn't work and I could not kill any of the PID)
$ ps auxw | grep post
Saw a slew postgres processes, which I then sudo kill <PID>
Everything working fine now.
For those using mac, this code worked for me like charm.
sudo pkill -u postgres
At a guess, something else had taken port 5432 so the app chose to run on 5433 instead.
Why not just connect to Pg on port 5433, if that's where it's running? You have a /tmp/.s.PGSQL.5432 socket file, so you can connect with psql -p 5433 for UNIX domain socket connections. It'll also be listening on the same port with TCP/IP, so you can use psql -h localhost -p 5433 for TCP/IP and have your apps connect to port 5433 instead of 5432.
Change your .psqlrc to set the new port as default and you can forget it isn't on the default port.
I had similar problem when trying to use postgresql with rails. Updating my Gemfile to use new version of gem pg solve this problem for me. (gem pg version 0.16.0 works). In the Gemfile use:
gem 'pg', '0.16.0'
then run the following to update the gem
bundle install --without production
bundle update
bundle install
Mine failed to start up suddenly, and when I checked Console.app I saw:
com.heroku.postgres-service:
FATAL: could not create shared memory segment: Cannot allocate memory
DETAIL: Failed system call was shmget(key=5432001, size=3874816, 03600).
HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space, or exceeded your kernel's SHMALL parameter. You can either reduce the request size or reconfigure the kernel with larger SHMALL. To reduce the request size (currently 3874816 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
The PostgreSQL documentation contains more information about shared memory configuration.
It turns out Postgres wouldn't start up because I had Wireshark (and X11) running. It worked fine after I quit Wireshark.
Good luck!
I resolved this problem by
Identifying what was running on port 5432 by using "netstat" in the CL, which was postgreSQL not Postgres
I located the directory that contained postgreSQL, which was root/Library/PostgreSQL
I ensured an instants of the application wasn't running via Activity Monitor
Then I deleted the folder and rebooted! Everything was fine!
Somehow I totally forgot that this socket file will be hidden because of the dot. Make sure you use ls -A /tmp/.s.PGSQL.5432 if you are checking to see if the socket is actually there.
You most likely has a PostgreSQL installed, deleted it and installed it again. PostgreSQL typically used port 5432 but if not available, increases to the next available one, in this case 5433. So, you probably chose this port on your second install.
I think you should check file:
/etc/services
and adjust rows below for your expected port number:
postgresql 5432/udp # PostgreSQL Database
postgresql 5432/tcp # PostgreSQL Database
After this you should restart your computer (simplest way).
I had the same issues:
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"?
In my case it was a corrupt host file on my mac. I was missing:
127.0.0.1 localhost
A quick easy way to check is to open terminal and type:
ping localhost
or:
scutil -r localhost
More info here
Hope it helps.
Netstat, ps aux, etc ... none showed 5432 in use. Checked /Library. Found PG9.6 old install still there. Did rm -rf and bang. Version 11 works fine.
I was losing my mind over this problem! i kept running
lsof -i | grep 5432
and nothing was showing up!
finally i ran it using sudo and a potgres client showed up.
so if anyone else has tried the lsof and went nowhere, try it with sudo.
sudo lsof -i | grep 5432
and then
sudo kill <the_pid_from_postgresql_found_from_lsof>
The same problem just happened to me. I had Postgres.app 9.2.4.1 running since a while. When I updated Mac OS X to 10.8.5, after the mandatory reboot it was not working anymore. I tried several things, including updating to 9.2.4.3 and neither after another reboot it was working.
I had to open the file /Users/$USER/Library/Application Support/Postgres/var/postgresql.conf and to:
uncomment line unix_socket_permissions = 0777
uncomment and adapt line unix_socket_directory = '/tmp'
After a restart, Postgres.app was running like a charm.
A file named "postmaster.pid" (in my $PGDATA directory) were preventing postgresql to start. It was a zombie file, placed there 10 days ago and when I brutally shut down the computer (pulling the plug, literally), no clean up process had the opportunity to remove that file.
beside all the helpful answers, you might also want to take a look at official page instructions :
postgressapp.com/documentation/remove.html
I had this same issue and mine was caused by some configuration issues I was having. Clearing the user configurations and reinstalling postgres in brew worked for me!
brew uninstall postgresql
rm -rf /usr/local/var/postgres
I ran the following:
psql --version then brew services stop postgresql#<VERSION>
finally, start your Postgres app on your Mac.

PostgreSQL permissions issue after upgrading to OS X Lion

After upgrading to Lion, I get the following error when trying to start up the Postgres server:
pg_ctl: could not open PID file "/usr/local/var/postgres/postmaster.pid": Permission denied
I also tried to re-run the initdb command, but ran into a similar problem:
initdb: could not access directory "/usr/local/var/postgres": Permission denied
If it matters, PostgreSQL was installed via Homebrew. Running brew info postgresql yields the expected results (version, summarized docs).
Well, it turns out the solution was pretty simple. I changed the group on /usr/local/var to staff (from wheel) and changed the ownership (chown -R) to my system account (from root).
After that, postgres started up fine.
I was a little nervous changing those permissions, but the only thing in my /usr/local/var was a postgres directory, so all should be well. If you have other directories/files in /usr/local/var, maybe don't use the -R flag when chown'ing?
The Homebrew ruby installer script changes the group of /usr/local/var to staff, so that must have gotten undone when upgrading to Lion. Not sure about the ownership being root instead of my system account though...
sudo chown -R $(whoami) /usr/local/var

Resources