不管怎么做,PostgreSQL都无法运行

我已经无法计算我第几次 PostgreSQL 没有运行了。
我没反省,就算多次没有运行,这样真是太蠢了,所以这次一定要记下来。
这是我自己完全设下的陷阱,自己反而落入其中,所以我觉得这个故事并不值得参考。

像往常一样使用 brew 进行安装,然后将长长的日志粘贴到终端上。

$ brew install postgresql
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
fasttext
==> Updated Formulae
pcre2 ✔                                 hugo                                    istioctl                                lz4
gmic                                    ios-webkit-debug-proxy                  libuv                                   zstd

==> Installing dependencies for postgresql: readline
==> Installing postgresql dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/9857a88bdb1a8ec96b986e99084ea823541d357d451a70a028e7c30410f918e9--readline-8.0.0.mojave.bottle.tar.gz
==> Pouring readline-8.0.0.mojave.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> Summary
?  /usr/local/Cellar/readline/8.0.0: 48 files, 1.5MB
==> Installing postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.2.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/991d29978b1e0603895b59851c553e41370fefedbb67797b6078ead33bccdba2--postgresql-11.2.mojave.bottle.tar.gz
==> Pouring postgresql-11.2.mojave.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/ecpg_config.h
/usr/local/include is not writable.

You can try again using:
  brew link postgresql
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
?  /usr/local/Cellar/postgresql/11.2: 3,186 files, 35.4MB
==> Caveats
==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

按照日志所述,设置用于readline的环境变量。

$ echo -e "export LDFLAGS=\"-L/usr/local/opt/readline/lib\"\nexport CPPFLAGS=\"-I/usr/local/opt/readline/include\"\nexport PKG_CONFIG_PATH=\"/usr/local/opt/readline/lib/pkgconfig\"" >> ~/.bash_profile
$ source ~/.bash_profile

通过 brew services 启动 postgres。

$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
$ brew services list
Name       Status  User       Plist
mysql@5.6  stopped
postgresql started ninjaninja /Users/ninjaninja/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
redis      started ninjaninja /Users/ninjaninja/Library/LaunchAgents/homebrew.mxcl.redis.plist

PostgreSQL 已经启动了(・`ω・´)

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

好的,出错了。又是这个常见的错误。每次都会出现,真是没反省呢。我真是个笨蛋。我们来解决吧。

嗯嗯。

当/主机以/开头时,会使用Unix域套接字进行连接。

那么解决方法是,在命令中加入 –host localhost 选项,强制通过TCP/IP连接。我在 Stackoverflow 上学到的。

$ psql -hlocalhost
psql: FATAL:  role "ninjaninja" does not exist

啊,要不用户输入的话。

$ psql -hlocalhost -Upostgres
dyld: lazy symbol binding failed: Symbol not found: _PQsetErrorContextVisibility
  Referenced from: /usr/local/opt/postgresql/bin/psql
  Expected in: /usr/lib/libpq.5.dylib

dyld: Symbol not found: _PQsetErrorContextVisibility
  Referenced from: /usr/local/opt/postgresql/bin/psql
  Expected in: /usr/lib/libpq.5.dylib

Abort trap: 6

这个错误是什么呢?(;゙゚’ω゚’):
问了世界上的朋友,完全找不到答案。这是怎么回事呀。然后我再次查看安装时的日志。

==> Pouring postgresql-11.2.mojave.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/ecpg_config.h
/usr/local/include is not writable.

出错了啊。好好注意一下。

/usr/local/include不可写。

唔。家里环境里没有/usr/local/include,怎么办啊?!
这是因为我之前的文章引起的问题呢:(;゙゚’ω゚’):

在这种情况下,我将 /usr/local/include 移动到了 /usr/local/include_old。

不要恢复/usr/local/include,我们会重新创建。这样会导致rbenv无法正常运行。

$ sudo mkdir /usr/local/include
$ sudo chown ninjaninja:admin /usr/local/include

然后重新安装PostgreSQL。

$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/11.2... (3,186 files, 35.4MB)

$ brew install postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.2.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/991d29978b1e0603895b59851c553e41370fefedbb67797b6078ead33bccdba2--postgresql-11.2.mojave.bottle.tar.gz
==> Pouring postgresql-11.2.mojave.bottle.tar.gz
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
?  /usr/local/Cellar/postgresql/11.2: 3,186 files, 35.4MB

嗨,这次一定没有错误。

$ psql -hlocalhost -Upostgres
psql (11.2)
Type "help" for help.

postgres=#

终于成功访问到了,我真的觉得自己太傻了。

广告
将在 10 秒后关闭
bannerAds