自制的海报坏了

我用自制程序在 Mac (10.10.1/Yosemite)上安装了 Postgreql 9.4.0,它不能工作。

我已经在 ~/Library/LaunchAgent 中创建了/usr/local/opt/postgreql/homebrew.mxcl.postgreql.plist 的软链接。

如果我尝试手动加载 postgres 我得到的消息,“操作正在进行中”

> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.0/homebrew.mxcl.postgresql.plist: Operation already in progress

然而,postgres 似乎没有运行。

> ps auxw | grep post
billmcn           670   0.0  0.0  2424272    452 s000  R+   10:12PM   0:00.01 grep post

我无法连接到命令行客户端。

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

据我所知,我已经尝试了在讨论这个问题的 其他 堆栈溢出线程上建议的所有修复程序:

  • 我已经卸载并重新安装了 postgres 以及附带的 Ruby gem,我的机器上没有 postgres 8.0版本。
  • 我已经验证了 psql 客户机程序是 Homebrew 安装的9.4.0版本,而不是 Mac 系统的二进制文件。
  • 我已经验证了/usr/local/var/postgres/postmaster. pid 不存在。
  • 我重新启动了机器。

早些时候我确实让自制的邮递员在这台机器上工作过。我认为它的破坏是从版本8升级到版本9,但我不确定。

我没有任何需要保存的数据库。我愿意和 Postgres 一起开始干净的生活,我现在只需要让它开始工作。有什么想法吗?


问题似乎是对/usr/local/var/postgres 目录的权限。下面是我的 var 目录在出现故障时的情况。

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 = “ bilmcn”)

我删除了/usr/local/var/postgres,卸载并重新安装 postgres,现在看起来是这样的。

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

我不知道它是如何进入这种状态的,因为我不记得有没有这个目录的权限,不过没关系。现在可以用了。

71129 次浏览

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

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.

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

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

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.

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!

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.

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.

Please read and follow the instructions.

  1. 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
  2. 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
  3. 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