繁体   English   中英

Postgres - 致命:数据库文件与服务器不兼容

[英]Postgres - FATAL: database files are incompatible with server

重新启动 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并且该文件仍然存在。

如果您最近将 postgres升级到最新版本,您可以运行以下命令来升级您的 postgres 数据目录,保留所有数据:

brew postgresql-upgrade-database

上面的命令取自brew info postgres的输出

如果您正在寻找核选项(删除所有数据并获取新数据库),您可以执行以下操作:

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

然后你需要rake db:setuprake db:migrate从 Rails 应用程序重新设置。

试试这个: 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 查看源代码

正如@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  

与这些答案( 12 )类似,我的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM