重新启动 MacBook Pro 后,我无法启动数据库服务器:
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"?
我检查了日志,并一遍又一遍地出现以下行:
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.0.4.
9.0.4 是预装在 mac 上的版本,9.2[.4] 是我通过 Homebrew 安装的版本。
如前所述,这曾经在重新启动之前工作,因此它实际上不是编译问题。我还重新运行了 initdb /usr/local/var/postgres -E utf8
,该文件仍然存在。
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
- 响应为 server starting
pg_ctl
的文件时,我很确定您会找到 2 个副本。与 which pg_ctl
匹配的将是旧版本,另一个将是新版本。
pg_ctl --version
给出 pg_ctl (PostgreSQL) 9.2.4
Server.app
中并将其隐藏以使用非默认端口和 unix 套接字目录。最后!
如果您最近将 postgres 升级到最新版本,您可以运行以下命令来升级您的 postgres 数据目录,保留所有数据:
brew postgresql-upgrade-database
上述命令取自 brew info postgres
的输出
如果您正在寻找核选项(删除所有数据并获取新数据库),您可以执行以下操作:
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
然后您需要在 Rails 应用程序中执行 rake db:setup
和 rake db:migrate
以重新设置。
createuser -s your_rails_app
来创建 rails 的 postgres 用户。请参阅stackoverflow.com/questions/11919391/…
mv /usr/local/var/postgres /usr/local/var/postgres_backup && initdb /usr/local/var/postgres -E utf8
!
试试这个:https://gist.github.com/joho/3735740
它对我来说非常有效。最后,它还会为您生成 2 个 bash 脚本来检查您的数据库并删除旧集群。非常棒。
请参阅:http://www.postgresql.org/docs/9.2/static/pgupgrade.html 以了解更多信息。
在互联网上找到,这个解决方案对我来说很好。
当我在升级到 OS X 10.10 Yosemite 后尝试启动 postgresql 服务器时,遇到了下一个问题:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
好的,让我们看一下服务器日志:
cat /usr/local/var/postgres/server.log
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.5.
因此,我们需要在升级 postgresql 后遵循几个步骤:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres92
brew update
brew upgrade postgresql
initdb /usr/local/var/postgres -E utf8
pg_upgrade -b /usr/local/Cellar/postgresql/9.2.3/bin -B /usr/local/Cellar/postgresql/9.3.5_1/bin -d /usr/local/var/postgres92 -D /usr/local/var/postgres
cp /usr/local/Cellar/postgresql/9.3.5_1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
rm -rf /usr/local/var/postgres92
而已。
如果要保留以前版本的 postgres,请使用 brew switch
:
$ brew info postgresql
postgresql: stable 10.5 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
Conflicts with:
postgres-xc (because postgresql and postgres-xc install the same binaries.)
/usr/local/Cellar/postgresql/9.6.3 (3,259 files, 36.6MB)
Poured from bottle on 2017-07-09 at 22:15:41
/usr/local/Cellar/postgresql/10.5 (1,705 files, 20.8MB) *
Poured from bottle on 2018-11-04 at 15:13:13
$ brew switch postgresql 9.6.3
$ brew services stop postgresql
$ brew services start postgresql
否则,请考虑使用此 brew 命令迁移现有数据:brew postgresql-upgrade-database
。查看the source code。
正如@Gowtham 提到的,您可以通过执行 brew 命令来解决此问题
brew postgresql-upgrade-database
上述命令取自 brew info postgres
的输出
但是,不要忘记在执行 postgres 服务之前停止它,使用以下命令:`brew services stop postgresql
您可能需要重新启动该服务。
所以命令的最终顺序是:
brew services stop postgresql
brew postgresql-upgrade-database
brew services start postgresql
与这些答案(1、2)类似,我的 Postgres 数据库文件在升级到 postgresql 13.3
后与我的 Postgres 版本不兼容。
不幸的是,升级我的 Postgres 数据目录失败了。
$ brew postgresql-upgrade-database
...
Setting next OID for new cluster
*failure*
Consult the last few lines of "pg_upgrade_utility.log" for
the probable cause of the failure.
Failure, exiting
Error: Upgrading postgresql data from 12 to 13 failed!
==> Removing empty postgresql initdb database...
==> Moving postgresql data back from /usr/local/var/postgres.old to /usr/local/var/postgres...
Error: Failure while executing; `/usr/local/opt/postgresql/bin/pg_upgrade -r -b /usr/local/Cellar/postgresql@12/12.7/bin -B /usr/local/opt/postgresql/bin -d /usr/local/var/postgres.old -D /usr/local/var/postgres -j 8` exited with 1.
我的解决方法是重新安装 postgresql 12.7
。
$ brew reinstall postgresql@12
$ brew services start postgresql@12
当我尝试使用 postgres11 安装卷启动 Postgres12 时,它发生在我身上。只需删除 postgres11 的已安装卷并重新启动对我有用。
以前我使用的是:
docker run -d --name my_database -v /Users/champ/postgres:/var/lib/postgresql/data -p 54320:5432 postgres:11
我删除 /Users/champ/postgres 并重新启动 postgres 12,使用
docker run -d --name my_database -v /Users/champ/postgres:/var/lib/postgresql/data -p 54320:5432 postgres:12
brew info postgres
会给您提示,例如 To migrate existing data from a previous major version of PostgreSQL run:
因此,在我的情况下,删除旧的 rm -rf /usr/local/var/postgres.old
并升级数据库 brew postgresql-upgrade-database
在 Docker 中运行新的 Postgres 并且您的旧卷未更新时也会发生这种情况。
如果您不需要保持数据最简单,请清除 docker 文件夹中的旧卷,在 Linux 中,此处为:
/var/lib/docker/volumes
一个陈旧的 postmaster.pid
文件对我造成了这种影响。
只需导航到您的 postgres 目录 /Users/st/Library/Application Support/Postgres/
,对我来说,就是:
cd '/Users/<username>/Library/Application Support/Postgres/var-10'
然后删除文件 postmaster.pid
。重新启动postgres,它会工作。
对我来说,使用这个命令:
brew install --build-from-source postgresql@12
然后启动 Postgres:
brew services start postgresql@12
您还可以检查端口 5432 是否正在侦听:
netstat -nl |grep 5432