638

After I did brew update and brew upgrade, my postgres got some problem. I tried to uninstall postgres and install it again, but it didn't work as well.

This is the error message. (I also got this error message when I try to do rake db:migrate)

$ 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"?

How can I solve it?

Mac version: Mountain lion.

homebrew version: 0.9.3

postgres version: psql (PostgreSQL) 9.2.1

And this is what I did:

$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.2.1...
$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.1.4...
$ psql --version
bash: /usr/local/bin/psql: No such file or directory
$ brew install postgresql
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.1/postgresql-9.2.1.tar.bz2
Already downloaded: /Library/Caches/Homebrew/postgresql-9.2.1.tar.bz2
......
......
==> Summary
/usr/local/Cellar/postgresql/9.2.1: 2814 files, 38M, built in 2.7 minutes
$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "laigary".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
homebrew.mxcl.postgresql: Already loaded
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.14.1
1 gem installed
$ psql --version
psql (PostgreSQL) 9.2.1
$ 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"?

Now, after I reinstalled homebrew, when I use $ psql, it doesn't show any error message.

But I run rake db:migrate in my Rails app, it shows:

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"?

/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `connect'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:319:in `checkout_new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `block (2 levels) in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `block in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in `block in connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:107:in `rescue in create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:51:in `create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (3 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (2 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `call'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:63:in `run'
/usr/local/bin/rake:32:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"riy_development", "pool"=>5, "username"=>nil, "password"=>nil}

Finally I've found a solution.

sudo mkdir /var/pgsql_socket/
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

This solution is a little tricky, but it works. Hope anyone has a better solution

Update

This works for me as well.

rm /usr/local/var/postgres/postmaster.pid
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Gary Lai
  • 6,543
  • 3
  • 17
  • 16
  • 4
    I am having your exact same problem, but your solution did not work for me. I think this is a permissions problem but I don't have the knowledge of file permissions to fix it. – ajbraus Feb 16 '13 at 22:59
  • For are those who install PG via Homebrew and are having problems, I found another answer. Just uninstall pg gem and reinstall with Homebrew configs. See the answer at http://stackoverflow.com/a/19609228/1072058. – zquintana Oct 26 '13 at 17:29
  • similar answer here - http://stackoverflow.com/questions/13573204/psql-could-not-connect-to-server-no-such-file-or-directory-mac-os-x – VelLes Mar 19 '14 at 20:33
  • 2
    Start postgres. – atw Feb 11 '17 at 13:06
  • 21
    Upgrade your existing to the newer version with the following command `brew postgresql-upgrade-database` –  Apr 06 '19 at 04:06
  • What worked for me: `brew services stop postgres && initdb && createdb $(whoami) && createuser -s postgres && brew services start postgres` – SamB Nov 09 '21 at 23:50
  • I had a similar problem. I accidentally deleted the default 'postgres' database. You can try this: sudo -d 'existing db like template01' -u postgres -h localhost; from there create db: create database postgres; then try: sudo -u postgres psql – Rakonjac May 19 '22 at 11:45
  • Just want to add this sneaky fix, since I ran into the same issue today: i forgot to `rails db:create` first. Whoops. – J.R. Bob Dobbs Oct 06 '22 at 21:34

66 Answers66

1249

Had a similar problem; a pid file was blocking postgres from starting up. To fix it:

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

and then all is well.


UPDATE:

For Apple M1 (Big Sur) users, do this instead:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql

UPDATE (Nov 15 2022):

For Apple M1 / M2 (Ventura) users, just do this:

$ bundle install (just to make sure pg is installed)
$ brew services restart postgresql
Timmy
  • 4,098
  • 2
  • 14
  • 34
jonathanpberger
  • 12,546
  • 1
  • 14
  • 2
  • 9
    This worked for me on OSX Mavericks after seeing this issue upon a update/restart. – eprothro Dec 11 '13 at 15:43
  • 1
    Thanks, this worked for me too. My PostgreSQL from Homebrew broke after I did a minor Maverick update to 10.9.2, maybe it was just a metter of cleaning the old .pid file. – VelLes Mar 18 '14 at 18:51
  • 3
    Using Yosemite it didn't work. Still the same error and the file no longer exists. But it used to work on OSX versions for me though... any help? "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"?" – betoharres Oct 21 '14 at 04:39
  • 2
    @BetuUuUu see this post for a solution on Yosemite. It's not pretty, but it works. http://stackoverflow.com/questions/25970132/pg-tblspc-missing-after-installation-of-os-x-yosemite – jstafford Oct 22 '14 at 17:30
  • 3
    @jstafford thanks, I already solved!:) The solution was to uninstall via homebew and then restart my computer right away, so then I would be able to reinstall postgres. The old version was running in the background, creating files and conflicts every time I tried to reinstall postgres. – betoharres Oct 23 '14 at 10:04
  • 310
    This didn't work for me on Mavericks: `rm: /usr/local/var/postgres/postmaster.pid: No such file or directory` – sscirrus Mar 24 '15 at 18:04
  • 18
    This worked for me, after erasing I had to start postgres. # pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start – Ricardo Castañeda Mar 26 '15 at 19:46
  • 20
    I recommend tailing the server log to make sure this is actually the issue: `tail /usr/local/var/postgres/server.log` – Josh Bodah May 07 '15 at 13:50
  • After Yosemite crashed and restarted, cannot connect to Postgres. your answer worked perfect. :) – Adolfo Jun 30 '15 at 21:22
  • Mac Yosemite 10.10.5 after battery died, rebooted, and xCode finished it's update - your answer still works beautifully ... thanks! – ea0723 Aug 14 '16 at 09:35
  • This does not work for mac users, check my answer below http://stackoverflow.com/a/42127625/3700798 – svelandiag Apr 07 '17 at 00:39
  • 1
    I don't see that pid file . I am using PostGreSQL version 9.6 – zulkarnain shah Sep 13 '17 at 10:54
  • 1
    Remember to restart the service after deleting the .pid – tomascharad Oct 11 '17 at 17:44
  • 1
    for those whose the command does not work, ensure first you can see invisible files, then check path /usr/local/var/ You may have multiple folders there for different versions of postgresql. Each could contain the postmaster.pid file that needs to be removed – Jerome May 02 '18 at 06:43
  • 3
    This followed by `brew services restart postgresql` worked for me – Chris Wendt Jun 30 '18 at 03:35
  • 1
    This worked for me after an accidental restart (battery died and I had to reboot). Thank you! – aronmoshe_m Dec 07 '18 at 05:46
  • should be `rm -rf ~/usr/local/var/postgres/postmaster.pid` – Fábio BC Souza Jul 06 '20 at 01:37
  • 1
    @FábioBCSouza No it should not! – ruohola Jul 23 '20 at 10:24
  • @ruohola, without recursive force I could not make it work. Using `rm -rf` the pid was deleted – Fábio BC Souza Jul 23 '20 at 11:37
  • 3
    Some forget about the version, so they will get `No such file or directory`. They should be more specific: `rm /usr/local/var/postgresql\@9.5/postmaster.pid` then `brew services restart postgresql@9.5` – MIA Aug 15 '20 at 15:51
  • Thanks ! For me I just needed to run the second command: 'brew services restart postgresql' – Heimson Oct 02 '20 at 14:24
  • If you installed a specific version of postgres using homebrew, the paths above might be different. If you run brew list | grep postgres and see postgresql@11 for example, then you would need to run $ rm /usr/local/var/postgresql@11/postmaster.pid $ brew services restart postgresql@11 – Patrick Feb 18 '21 at 15:20
  • I just wanna say this is the worse errors I get every single time there's an OS update, Postgres update, computer restart or anything in between. It's incredible that I need to go seeking for the same solution over and over. Had to vent somewhere. – Gustavo Matias Mar 09 '21 at 14:11
  • Thank you! Directly restarting for me didn't work, had to remove the pid file. – Aqeeb Imtiaz Harun Apr 06 '21 at 18:51
  • 2
    As @MIA says, sometimes you need to be version specific. I needed `rm /opt/homebrew/var/postgresql@12/postmaster.pid` and `postgresql@12` (macOS Big Sur on M1) – David Aldridge Jun 12 '21 at 09:13
  • Works on Catalina as well! – Ivan Hanák Jul 08 '21 at 07:37
  • I just had to `brew services restart postgresql` – Fernando Nov 25 '21 at 11:10
  • 1
    My `postmaster.pid` file was in `/usr/local/var/postgresql@13/postmaster.pid` rather than the directory above - so you might need to do `find / -name postmaster.pid` to get the right directory. – DaveR Mar 22 '22 at 12:46
  • This worked for me in Monterey. – Srinivas Reddy Thatiparthy Jul 19 '22 at 10:12
362

This sometimes happens when brew does a postgres upgrade, causing the data files to become incompatible with the new server.

In my case, it happened when upgrading from 9.3 to 9.4.

OS X/Homebrew:

Try running postgres -D /usr/local/var/postgres -- it will give you a much more verbose output if postgres fails to start. Or open the log file at /usr/local/var/log/postgres.log (/opt/homebrew/var/log/postgres.log on Mac M1) and look for the line "FATAL: database files are incompatible with server"

In my case, running rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8 removed my old databases and then reinitialized the postgres db schema. (THIS WILL DESTROY YOUR DATA)

Thanks to https://github.com/Homebrew/homebrew/issues/35240 for that solution. Full instructions for a thorough re-install can be found here: How to completely uninstall and reinstall Homebrew Postgres - Test Double Blog (Again, if you're on an M1 Mac then substitute the /opt/homebrew/var path wherever it says /usr/local/var)

After regenerating my databases (with rake db:create) everything worked fine again.

Finally, links in the comments point to this possible solution that preserves your data, but I haven't tried it: How to upgrade PostgreSQL from version 9.6 to version 10.1 without losing data - Stack Overflow

Yarin
  • 173,523
  • 149
  • 402
  • 512
FreePender
  • 4,770
  • 2
  • 18
  • 15
  • 1
    This worked for me with a homebrew installed postgres on Yosemite. – cpursley Feb 01 '15 at 18:30
  • 7
    If running `postgres -D /usr/local/var/postgres` tells you that the data directory was initialized with an older, incompatible version of Postgres, and you *don't* want to lose your local data, you might try [this thread](http://stackoverflow.com/q/24379373/4376) – Nathan Long Mar 27 '15 at 10:34
  • After the above command was run, I ran: `pg_ctl -D /usr/local/var/postgres -l logfile start` and then my server started in the background –  Feb 11 '16 at 16:19
  • 5
    Use `brew services start postgres` on OSX to start postgres on background instead of `postgres -D /usr/local/var/postgres` – Mike Andrianov Apr 08 '16 at 10:19
  • 1
    Just a head's up that this deletes your databases, which may not be what you want (or you might not care if this is all dev all the time) – stujo Aug 10 '16 at 17:30
  • Worked for me. I lost all my local databases but it fix it so it works. This i fine by me since this has locally. – Simon1901 Aug 29 '16 at 09:00
  • `FATAL: database files are incompatible with server` was the reason. I don't know how to fix that yet, but at least I know what's going on. Thanks! – coffekid Jan 06 '17 at 23:48
  • I just accidentally upgraded my postgresql from 9.5 to 10.1 and improperly shutdown the pc. For the record, here's the log: postgres -D /usr/local/var/postgres \n `2018-02-12 11:31:23.676 +08 [2526] FATAL: database files are incompatible with server 2018-02-12 11:31:23.676 +08 [2526] DETAIL: The data directory was initialized by PostgreSQL version 9.5, which is not compatible with this version 10.1. – Charlie Feb 12 '18 at 03:42
  • 1
    @coffekid I had the same issue, and fixed it with the instructions here: https://stackoverflow.com/questions/24379373/how-to-upgrade-postgresql-from-version-9-6-to-version-10-1-without-losing-data – Micah Walter Feb 28 '18 at 15:54
  • 1
    WOW! After trying everything, this `rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8` finally worked for me. Thank you! – Nuha Dec 06 '19 at 23:28
  • Command `rm -rf /usr/local/var/postgres` throw me an error "folder doesn't exists" but then I ran `initdb /usr/local/var/postgres` it worked for me! Thanks – Salvador Nava Aug 16 '21 at 01:29
  • 1
    Worked for Postgres 13 -> 14 upgrade on macOS 10.15 in which other listed answers did not. – phoenix Nov 17 '21 at 12:44
  • Note that after this you may need to recreate the postgres role `/usr/local/opt/postgres/bin/createuser -s postgres` and your personal database `psql -U postgres` then `create database` with your role name. – Rick Westera Nov 23 '21 at 20:52
  • postgres -D /usr/local/var/postgres turned out to show postmaster.pid already exists – Daniel Viglione Aug 09 '22 at 17:05
91

Found a solution that worked for me here:

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

You basically run the following command to manually start the server:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Community
  • 1
  • 1
Mosab Sasi
  • 1,130
  • 8
  • 11
69

If installing and uninstalling postgres with brew doesn't work for you, look at the logs of your postgresql installation or:

postgres -D /usr/local/var/postgres

if you see this kind of output:

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 9.6.1.

Then try the following:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

Then start the server:

pg_ctl -D /usr/local/var/postgres -l logfile start

Source

svelandiag
  • 4,231
  • 1
  • 36
  • 72
64

Upgrading the database works for me

brew postgresql-upgrade-database
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Tomer G
  • 672
  • 6
  • 8
  • 2
    ✅⭐️Thank you Tomer! This worked for me 6/2/20 upgrading from 9.4 to 12 on macOS Mojave 10.14.6 – Rob Bednark Jun 02 '20 at 21:43
  • 1
    After changing Ruby version, I didn't noticed but my Postgresql installation changed versions and only with this command could have it running again. – Francisco Quintero Aug 03 '20 at 21:15
  • this worked for me also. 2 days back I updated homebrew which caused my postgres but after updating postgressql , its working fine. Thanks again – Sachin Jan 18 '21 at 06:21
  • `No such file or directory @ rb_sysopen - /usr/local/var/postgres/PG_VERSIO` – shah Jul 20 '21 at 05:30
  • Thank you so much! This worked for me I think brew automatically updated lot of packages when I installed some other package. – Vikas Pal Dec 02 '21 at 20:53
  • Thank you Tomer! This worked for me too, upgrading from 13 to 14 on Mac Monterey 12.3.1. Don't you love one line solutions? – Pshemski May 04 '22 at 16:08
40

On Yosemite, if the pid file is blocking Postgres from starting and you have a launchctl daemon trying (and failing) to load the database daemons, then you'll need to unload the plist file:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Then remove the pid file

$ rm /usr/local/var/postgres/postmaster.pid

Then reload the launchctl daemon

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Ryan Atallah
  • 2,977
  • 26
  • 34
36

For anyone reading this and using Postgres.app, you may need host: localhost in your database.yml. http://postgresapp.com/documentation#toc_3

David Winiecki
  • 4,093
  • 2
  • 37
  • 39
  • Doing this changes the connection from a socket to a TCP connection which may have negative implications. See http://stackoverflow.com/questions/6770649/repairing-postgresql-after-upgrading-to-osx-10-7-lion/11678981#comment8036510_6770734 – Brett Bim Mar 23 '14 at 12:14
  • 1
    Hmm, actually, if you downvote... Maybe don't say why? http://meta.stackoverflow.com/questions/252297/how-should-i-handle-downvote-complaints#answer-252314 Or maybe say why, but log out first so you're anonymous. – David Winiecki Apr 30 '14 at 19:17
  • I think the down vote is because the original question wasn't for the Postgres.app but Homebrew. So while the answer might be useful somewhere, it isn't answering the question. – Brian Dear Nov 16 '14 at 09:56
  • The link doesn't seem to go anywhere specific (and searching "localhost" on the past has no results) but this just fixed the issue for me after an hour of searching. – sixty4bit Aug 21 '17 at 03:52
29
brew services start postgres 

worked for me!

rld
  • 2,603
  • 2
  • 25
  • 39
  • This worked for me. For some reason when I updated brew I was getting the OP error. I explicitly upgraded postgres with that above command and that seemed to work. – stingMantis Aug 15 '22 at 15:17
19

The problem can also be attributed to a crashed process that left postmaster.pid file behind. I do this and work's:

$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
$ brew services start postgresql
rld
  • 2,603
  • 2
  • 25
  • 39
18

The problem is because there is already a running service on the port 5432 and we cannot establish psql socket connection through this port.

I removed the socket file

rm -rf /tmp/.s.PGSQL.5432/

Then I reinitialized postgres services

postgres -D /usr/local/var/postgres

This worked for me.

Abdullah Gürsu
  • 3,010
  • 2
  • 17
  • 12
17

Check that the socket file exists.

$ ls -l /tmp/.s.PGSQL.5432
srwxrwxrwx  1 you  wheel  0 Nov 16 09:22 /tmp/.s.PGSQL.5432

If it doesn't then check your postgresql.conf for unix_socket_directory change.

$ grep unix_socket /usr/local/var/postgres/postgresql.conf
#unix_socket_directory = ''     # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
jrwren
  • 17,465
  • 8
  • 35
  • 56
  • 1
    This is my computer response. `$ ls -l /tmp/.s.PGSQL.5432 ls: /tmp/.s.PGSQL.5432: No such file or directory $ grep unix_socket /usr/local/var/postgres/postgresql.conf #unix_socket_directory = '' # (change requires restart) #unix_socket_group = '' # (change requires restart) #unix_socket_permissions = 0777 # begin with 0 to use octal notation` – Gary Lai Nov 17 '12 at 15:18
  • 29
    Finally I find the solution. `$ mkdir /var/pgsql_socket/` `$ sudo mkdir /var/pgsql_socket/` `$ ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/` This solution is little tricky, but it works. Hope anyone have a better solution – Gary Lai Nov 17 '12 at 16:49
  • nice job @GaryLai. Very strange that I did not have to do the same with my homebrew postgresql – jrwren Nov 21 '12 at 15:19
  • 2
    this may help some folks out there: https://github.com/mxcl/homebrew/issues/14527 – AdamT Feb 20 '13 at 04:24
16

The most fool proof way around this is to do

brew reinstall postgresql

This will keep your user permission etc all intact and everything is reset to new. Works all the time !

attaboyabhipro
  • 1,450
  • 1
  • 17
  • 33
  • This is worked but i recommend if `rm /usr/local/var/postgres/postmaster.pid` on mac os not worked then use this command. – Yaseen Ahmad Jun 15 '20 at 12:00
  • Exactly, I tried deleting postmaster.pid but I wasn't there, so I tried this solution and it recreated that file and now it works fine. – user309838 Feb 22 '23 at 17:13
11

In my case, the trouble was caused by Mac OS updating. Upgrading PostgreSQL solved the issue.

# upgrade database version solved the trouble
$ brew postgresql-upgrade-database

However, this error is quite common and has multiple possible reasons, you shouldn’t fully rely on my solution above.

Hany Moh.
  • 949
  • 1
  • 11
  • 11
  • This seemed to work well for big sur. note that depending on your specific version of postgresql from brew, and OS --any of the solutions involving deleting files from specific paths may not have the right path. In my case even when I found the right path it still failed--but this worked. – Zargold Nov 08 '21 at 15:35
10

For MacOS BigSur, homebrew postgres:

  • rm /opt/homebrew/var/postgres/postmaster.pid
  • brew services restart postgresql
Carlos Gómez
  • 453
  • 7
  • 16
9

This worked for me . I didn't have to remove any file

brew postgresql-upgrade-database

  • This was also my issue; my data directory was of an older version than the server, due to brew updating it. You can see if this is the problem by looking at the logs: `postgres -D /usr/local/var/postgres`. If you need to upgrade, you'll see something like, `The data directory was initialized by PostgreSQL version 13, which is not compatible with this version 14.2.` – Mike Williamson Mar 26 '22 at 11:13
8

This is actually what you are supposed to do:

you should instead look at /usr/local/var/postgres/postmaster.pid

and then look at the first line of the file - this is the bad PID

Run

ps aux | grep <PID>

for example:

ps aux | grep 12345

then do

kill <PID>

for example

kill 12345

Assuming it's still running

https://superuser.com/questions/553045/fatal-lock-file-postmaster-pid-already-exists

do not listen to the accepted answer it is bad and will corrupt your data!!!

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
user1596067
  • 91
  • 2
  • 4
7

Changing postresql or database.yml config settings, changing $PATH, or creating symlinks were all unnecessary for me. All I needed to do was gem uninstall pg and then bundle (or gem install pg).

The issue was that the pg gem had been installed before homebrew postgres, so was picking up the settings from the version of postgres that comes with MacOS. Reinstalling it (and thus rebuilding the native extension) fixed the problem.

John
  • 29,546
  • 11
  • 78
  • 79
  • Just wanted to say thanks. I just upgraded PostgresApp on macOS and although I could connect using psql, I couldn't connect via rails. uninstalling and reinstalling the pg gem was just the ticket! :) – craig1410 Feb 13 '18 at 01:09
7

This happened to me when I upgraded from 9.3.4 to 9.5 as the databases are incompatible without upgrading.

I used pg_upgrade as follows:

Stop postgres

$ brew services stop postgresql

Upgrade the databases:

$ pg_upgrade \
   -d /usr/local/var/postgres \
   -D /usr/local/var/postgres9.5 \
   -b /usr/local/Cellar/postgresql/9.3.4/bin/ \
   -B /usr/local/Cellar/postgresql/9.5.0/bin/ \
   -v

Archive the old databases:

 $ mv /usr/local/var/postgres /usr/local/var/postgres9.3.save
 $ mv /usr/local/var/postgres9.5 /usr/local/var/postgres

Restart postgres:

 $ brew services start postgresql

Updated Gems (for rails / active record) :

 $ gem uninstall pg
 $ gem uninstall activerecord-postgresql-adapter
 $ bundle install
stujo
  • 2,089
  • 24
  • 29
  • These instructions are also at https://collectiveidea.com/blog/archives/2016/01/08/postgresql95-upgrade-with-homebrew , and still worked for me today when upgrading from 9.4.0 to 9.6.3. I didn't need to do anything to the .pid file manually. – Jan Hettich Jul 22 '17 at 07:48
6

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 this also doesn't work then run -- sudo chmod 700 /usr/local/var/postgresql@9.6

Piyush Sawaria
  • 121
  • 1
  • 5
5

Psql option

-h hostname --host=hostname

: Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix-domain socket.

$ grep "port\|unix_socket" /etc/postgresql/9.1/main/postgresql.conf
port = 5433                                         # (change requires restart)
unix_socket_directory = '/var/run/postgresql'       # (change requires resta

$ netstat -nalp | grep postgres
unix  2      [ ACC ]     STREAM     LISTENING     106753   4349/postgres       /tmp/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     10377 1031/postgres       /var/run/postgresql/.s.PGSQL.5433

Run psql with -host Option

$ psql -p 5433 -h /var/run/postgresql

No need to make a soft link

Juno Choo
  • 51
  • 1
  • 1
5

If postgres was installed using homebrew, you can fix this by running:

brew link postgres
Cl Local
  • 109
  • 2
  • 3
5

This worked for me (as a mix of previous answers):

$ rm /usr/local/var/postgres/postmaster.pid

$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Source: https://coderwall.com/p/zf-fww/postgres-on-osx-with-homebrew-not-running-after-osx-crash

Ramon Diogo
  • 1,630
  • 2
  • 14
  • 9
5

So i stubled upon this after the rails db:create command. Setting up the environment in a macOS Catalina 10.15.3.

First thing that i checked was the flow that got me here. After ensuring that that all things had went smoothly and there was no error that might had escaped my mind i tried the most popular solutions from here but none of the seemed to work.

So far the only error i was seeing was the

$ 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"?

So i needed some more specific information about what was happening. Due to that reason i decided to look at the postgres logfile which is located at

/usr/local/var/log/postgres.log

So after opening the log i saw this error

LOG: starting PostgreSQL 12.2 on x86_64-apple-darwin19.3.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit LOG: could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known WARNING: could not create listen socket for "localhost" FATAL: could not create any TCP/IP sockets LOG: database system is shut down

So this is a bit more explanatory and specific. The problem is something about that PostgreSQL cannot "see" and resolve the localhost server.

So next thing i did was to check the /etc/hosts file whose default contents should look like this:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1          localhost 

After comparing the above with mine i saw that in mine this line was different and commented (!).

#::1             localhost

So i removed the # symbol in front of the line saved the file and re run the

rails db:create 

and the database was succesfully initiated.

dimivrn
  • 61
  • 3
  • 5
  • God bless you. Other answers are not working for me and there's no way I could see a postmaster.pid. I have other error but thank god for hinting me on the log file to inspect :. `/usr/local/var/log/postgres.log`. – nayiaw Feb 10 '21 at 13:59
5

Solution for MacOS M1 Monterey

rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
Khoi Ngo
  • 941
  • 8
  • 16
4

Came across this issue too on MacOS Sierra and when we ran pg_ctl as described above we then had the following error pg_ctl: no database directory specified and environment variable PGDATA unset. So we followed the steps here which solved our issue, namely:

mkdir ~/.postgres

initdb ~/.postgres

pg_ctl -D ~/.postgres start

Austin
  • 6,241
  • 5
  • 17
  • 12
4

I got same issue because I'm using a wrong Postgres's username in code. I logged into postgres psql -d postgres and enter \du to take role name and correct Postgres's username.

So when you guys face this issue, you guys need to make sure you're using correct Postgres username, password, hostname and database...

Hope this will help anyone

Brilliant-DucN
  • 706
  • 8
  • 12
4

If you shut down your system without quitting psql, postgres would not have removed some files.

I didn't find the file postmaster.pid in the location usr/local/var/postgres

So I did the below:

brew services start postgresql

The above command should let you start postgres

Community
  • 1
  • 1
Ashwin
  • 41
  • 5
3

I ran into this issue after trying to restore/drop/create a db while other processes were accessing them. MacOSX/Homebrew fix was:

  1. close all other accessing processes rails server, rails console, guard, etc...
  2. load/unload using the commands found in brew info postgres
  3. run restore/drop/create from before
TomFuertes
  • 7,150
  • 5
  • 35
  • 49
3

FWIW this happened to me today, but what happened was that I was running Ubuntu updates at the time, which were likely updating Postgres. Once the update completed, I was able to connect without a hitch.

For completeness' sake, I was trying to retrieve records from the database from a Rails console:

development (main):0 > a = MyModel.find 73694
PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Ruy Diaz
  • 3,084
  • 24
  • 36
3

It looks like your psql doesn't run. You should run it before connect. You can do that using Postgres.app for Mac OS only. (Download and install this app http://postgresapp.com) Open the app, and you have a PostgreSQL server ready and awaiting new connections. Close the app, and the server shuts down. You also can find this info here http://www.postgresql.org/download/macosx/. Hope this will help you.

lisyk
  • 81
  • 3
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Kmeixner Jun 09 '15 at 21:23
  • The question was specifically referring to a homebrew installation and not a Postgres.app install. While this answer is helpful, it doesn't answer the question. It would be like saying "Use MySQL instead." – Brian Dear Aug 26 '15 at 19:42
3

It is because a previous server a still running, try closing everything down and re-running your application.

3

For those who use this command and doesn't work or the file is not there and are using Ruby on Rails

rm /usr/local/var/postgres/postmaster.pid

Or any other command and just keep on failing.

I solved this problem uninstalling with Brew. I had to uninstall with brew 2 times, because at the first uninstall there will remain another version of postgresql, with the second uninstall the process will be completed.

Install postgresql with Brew

Then drop, create and migrate the data bases of the project

(Don't forget to start the postgresql server)

3

¿Are you recently changed the pg_hba.conf? if you did just check for any typo in:

"local" is for Unix domain socket connections only

local all all password

IPv4 local connections:

host all all 127.0.0.1/32 password

IPv6 local connections:

host all all ::1/128 password

Sometimes a simple mistake can give us a headache. I hope this help and sorry if my english is no good at all.

3

This happens when postgres server is not running. Steps to properly install Postgres via Homebrew on MAC :

  1. brew install postgres

  2. initdb /Users/<username>/db -E utf8 [This initializes postgres to use the given directory as the database directory. Normally it is not adviced to use the user directory for database storage. Edit sudoers file to add initdb and similar commands and then run initdb on /usr/local/var/postgres]

  3. pg_ctl -D /Users/<username>/db -l logfile start [After getting success with step 2 it will prompt to run step 3. This command manually starts the server.]

Souvik Das
  • 51
  • 1
2

I got this same error. Turns out postgres just wasn't running at all (it is usually always running in the background, but for whatever reason it wasn't today).

If this is the case, just type postgres in the command line of your project directory

Benjamin Dunphy
  • 869
  • 1
  • 9
  • 15
2

After a tremendous amount of back and forth, it really came down to the pg gem version I was using. On mavericks, pg version 0.15.1 would not connect to port 5432 but version 0.17.1 works just fine - very odd.

Anthony
  • 15,435
  • 4
  • 39
  • 69
2

For me it was an apache upgrade that caused the problem. I could still run psql in console or call db directly from kdevelop. Also it worked to add "host=localhost" to connection string.

BUT the real problem was that apache had changed to private tmp.

Solution: Update /usr/lib/systemd/system/apache2.service and change PrivateTmp=true to PrivateTmp=false.

I am working on OpenSuse OS, but I guess that something similar could happen on Mac.

kaah
  • 121
  • 1
  • 3
2

Had the same problem. Upgrading DB did the trick! postgresql-upgrade-database

anna
  • 65
  • 6
2

I simply restarted postgres:

brew services restart postgresql

There was no need to remove postmaster.pid.

Sajad Rastegar
  • 3,014
  • 3
  • 25
  • 36
2

This worked for me: rm /usr/local/var/postgres/postmaster.pid

Thomas Van Holder
  • 1,137
  • 9
  • 12
2

If you're on a M1 mac and you installed postgresql with the M1 version of homebrew, this shloud work:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql
1

I found that after installing the latest patch to Postgres 9.6, all my executables (postgres, psql, pg_dump, pg_restore etc) got dropped and I had to relink them. Fortunately Homebrew can do this for you:

brew link --force postgresql # (or in my case, `postgresql@9.6`)
PJSCopeland
  • 2,818
  • 1
  • 26
  • 40
1

Try

sudo systemctl start postgresql@12-main

Here my postgresql version is 12.

Mustafa
  • 5,624
  • 3
  • 24
  • 40
1

I was working on a project on my MacBook M1 (Air) and had opened some tabs and suddenly my Postgres server stopped and starting giving the below error :

psql: could not connect to server: Connection failed Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"

I tried many solutions like ;

  1. updating brew and Postgres.
  2. restarting servers.
  3. Tried to kill the process from the command line.

But sadly , nothing worked for me.

And Finally, I thought of restarting my mac . And to my surprise, the postgres server started working pretty much fine the way it used to do before. I think there must have been some problem with the Postgres directories. But I am glad that , it worked.

I recommend you, to go through the above-mentioned options first before restarting.

Thanks.

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

For Mac OS Catalina, this worked

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql
1

For macOS Monterey, I used brew services restart postgresql.

brew install postgresql
createdb mydb
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

brew services restart PostgreSQL
==> Successfully started `postgresql` (label: homebrew.mxcl

shin
  • 31,901
  • 69
  • 184
  • 271
1

I solved this problem in ubuntu by simply restaring the computer. The reason of this problem for my case was that the postgres was not stopped gracefully (via sudo kill).

0

The Cause

Lion comes with a version of postgres already installed and uses those binaries by default. In general you can get around this by using the full path to the homebrew postgres binaries but there may be still issues with other programs.

The Solution

Run this script:

#!/bin/sh

BREW_POSTGRES_DIR=`brew info postgres | awk '{print $1"/bin"}' | grep "/postgresql/"`
LION_POSTGRES_DIR=`which postgres | xargs dirname`
LION_PSQL_DIR=`which psql | xargs dirname`

sudo mkdir -p $LION_POSTGRES_DIR/archive
sudo mkdir -p $LION_PSQL_DIR/archive

for i in `ls $BREW_POSTGRES_DIR`
do
    if [ -f $LION_POSTGRES_DIR/$i ] 
    then
        sudo mv $LION_POSTGRES_DIR/$i $LION_POSTGRES_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_POSTGRES_DIR/$i
    fi
    
    if [ -f $LION_PSQL_DIR/$i ] 
    then
        sudo mv $LION_PSQL_DIR/$i $LION_PSQL_DIR/archive/$i
        sudo ln -s $BREW_POSTGRES_DIR/$i $LION_PSQL_DIR/$i
    fi  
done

Via

http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/

max pleaner
  • 26,189
  • 9
  • 66
  • 118
SocoM
  • 332
  • 4
  • 19
0

Just two steps to run the database after the Installation (Before that ensure your logged as postgres user)

Installed-Dirs/bin/postmaster -D Installed-Dirs/pgsql/data

For an example:

[postgres@localhost bin]$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data

Step-2 : Run psql from the Installed path (To check where you installed '#which postgres' will use to find out the installed location)

[postgres@localhost bin]$ psql 
Community
  • 1
  • 1
0

I used the Bitnami stack and installed as non-root user psql. On using psql I did receive the mentioned error.

Turns out, there are 2 versions of psql

  1. a root level psql prepackaged in the linux distro. This points to /usr/bin/psql
  2. a non-root level psql installed by the Bitnami stack. This points to /bitnami/postgresql/postgresql/bin/psql

You might want to change the alias of the psql, so that it points to the non-root user.

alias psql='/bitnami/postgresql/postgresql/bin/psql'

The above worked for me on doing psql -U postgres

Thyag
  • 1,217
  • 13
  • 14
0

The data directory contains an old postmaster.pid file / The data directory contains an unreadable postmaster.pid file PostgreSQL puts a file named postmaster.pid in the data directory to store the process id of the PostgreSQL server process. If PostgreSQL crashes, this file can contain an old pid that confuses PostgreSQL. You can fix this issue by deleting the postmaster.pid file. However, you must make sure that PostgreSQL is really not running. Open Activity Monitor and make sure that there are no processes named ‘postgres’ or ‘postmaster’.

If you delete the postmaster.pid file while PostgreSQL is running, bad things will happen.

Source: https://postgresapp.com/documentation/troubleshooting.html

Selv
  • 13
  • 2
0

If you are facing the issue while upgrading Postgres (or after upgrading), please follow the steps mentioned here to safely transfer data between versions, which will resolve the issue:

Upgrade PostgreSQL 9.6.5 to 10.0 using Homebrew (macOS)

0

One of the cause will be make sure the size of the disk is not full execute the command postmaster -D path of the postgres

Ajay Takur
  • 6,079
  • 5
  • 39
  • 55
0

For us, we had to define localhost in the psql command like the following:

psql -h localhost -U postgres

Not sure why the -h doesn't default to localhost...

hooknc
  • 4,854
  • 5
  • 31
  • 60
0

I had to fix the permissions for a directory:

sudo chmod 0750 /usr/local/var/postgres
David Morales
  • 17,816
  • 12
  • 77
  • 105
0

What worked for me I had 2 versions of PostgreSQL while running brew services list

Name           Status  User           Plist
consul         stopped                
docker-machine stopped                
mysql          stopped                
postgresql     started homebrew.mxcl.postgresql.plist
postgresql@9.6 stopped                
redis          stopped                
runit          stopped                
unbound        stopped                
vault          stopped 

and just launched the other version brew services start postgresql@9.6

0

In my case, I was running other project using docker-compose and I had to stop postgresql with sudo service postgresql stop && docker-compose up -d postgres && docker-compose up rails

To fix this error in my new project, I just had to run: sudo service postgresql restart and then it just fixed it.

0

I have macOS Catalina (10.15.6) and PostgreSQL 12.

When I run brew services start postgresql, brew said it start PostgreSQL without problem, but it does not show when I get ps auxw | grep post.

To solve it, I install Postgres.app from this link, and install and press Init button.

It will find your databases and show it to you.

Esmaeil
  • 558
  • 5
  • 11
0

MacOS Big Sur seemed to renamed the Application\ Support folder to ApplicationSupport. So the updated Path would be:

/Users/<USERNAME>/Library/ApplicationSupport/Postgres/var-10 
(or "/var-<YOUR PG VERSION>")
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
0

This is how it fixed this error for me on my MacOS (I had used homebrew to install postgres) :

> cd /opt/homebrew/var
> rm -rf postgres
> initdb ./postgres -E utf8
> pg_ctl -D ./postgres -l logfile start
> rm postgres/postmaster.pid
> brew services restart postgresql
> createdb $(whoami)
0

Quickest solution which worked for me:

  1. Find the postgres process that's running already in the port configured for postgres
lsof -i:5432
  1. Find the process id of postgres and kill them
kill -9 <pid>
ArunSelvam P M
  • 429
  • 5
  • 6
0

I've had same problem but the solutions did not work. I've finally fixed it with updating my postgres db:

brew postgresql-upgrade-database
0

When I started the course they mentioned I needed to set_pg, so I need to do that every time I create a new terminal window

$set_pg
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
greta
  • 1
  • 1
0

For MacOS Monterey and Postgresql@14 (the path is different):
rm /usr/local/var/postgresql@14/postmaster.pid
brew services restart postgresql

Max Berdnikau
  • 33
  • 1
  • 1
  • 6
0

For me, upgrading using brew works fine.

  • Config: Mac OS - Monterey M1
brew postgresql-upgrade-database
Satendra Rawat
  • 1,264
  • 11
  • 23
0

I tried almost all of these answers and this is what finally worked for me to get postgres to start

  • tail the logs for postgres.
    • the path needs to be updated depending on where postgres is installed, and your version. I am using postgresql@14 on an m1 Monterey and installed it with homebrew.

    • i finally found the path i needed to look at using this article.

    • tail /opt/homebrew/var/log/postgresql@14.log

output shows this:

2023-02-03 15:33:49.294 CST [82651] FATAL:  could not open directory "pg_notify": No such file or directory
2023-02-03 15:33:49.294 CST [82651] LOG:  database system is shut down
  • go to the / directory and cd opt/homebrew/var/postgresql@14

  • create the missing directory (maybe this is a different directory for you)

    • mkdir pg_notify
  • repeat this process for all missing directories.

    • I needed to mkdir for pg_tblspc, pg_replslot, pg_twophase, pg_stat_tmp, pg_logical/snapshots, pg_logical/mappings, pg_commit_ts, pg_snapshots, & pg_commit_ts but i recommend you specifically run the tail command each time to make sure you are not missing different directories & files than me.

finally after running the tail command repeatedly after creating each missing directory, I got this output.

2023-02-03 15:49:18.909 CST [85772] LOG:  redo done at 0/17211D8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2023-02-03 15:49:18.914 CST [85771] LOG:  database system is ready to accept connections