如何将PostgreSQL从9.6版升级到10.1版而不会丢失数据?

问题描述 投票:185回答:14

我正在将PostgreSQL数据库用于我的Ruby on Rails应用程序(在Mac OS X 10.9上)。

有关如何升级PostgreSQL数据库的详细说明吗?

我担心我会破坏数据库中的数据或弄乱它。

postgresql upgrade
14个回答
384
投票

假设您已使用home-brew安装和升级Postgres,您可以执行以下步骤。

  1. 停止当前的Postgres服务器: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  2. 初始化一个新的10.1数据库: initdb /usr/local/var/postgres10.1 -E utf8
  3. 运行pg_upgrade(注意:如果你从以下的东西升级,请更改bin版本): pg_upgrade -v \ -d /usr/local/var/postgres \ -D /usr/local/var/postgres10.1 \ -b /usr/local/Cellar/postgresql/9.6.5/bin/ \ -B /usr/local/Cellar/postgresql/10.1/bin/
  4. 将新数据移动到位: cd /usr/local/var mv postgres postgres9.6 mv postgres10.1 postgres
  5. 重新启动Postgres: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  6. 检查/usr/local/var/postgres/server.log以获取详细信息并确保新服务器正常启动。
  7. 最后,重新安装导轨pg gem gem uninstall pg gem install pg

我建议您花一些时间阅读PostgreSQL documentation,以便准确了解您在上述步骤中所做的工作,以尽量减少挫折。


1
投票

在Windows上,我在尝试使用pg_upgrade时遇到了不同的错误消息。

为我节省了大量时间:

  1. 备份DB
  2. 卸载PostgreSQL的所有副本
  3. 安装9.5
  4. 恢复数据库

0
投票

我的解决方案是将这两种资源结合起来:

https://gist.github.com/tamoyal/2ea1fcdf99c819b4e07d

http://www.gab.lc/articles/migration_postgresql_9-3_to_9-4

第二个比第一个更有帮助。另外,不要按照原来的步骤进行操作,因为有些步骤是不必要的。此外,如果您无法通过postgres控制台备份数据,您可以使用替代方法,并使用pgAdmin 3或其他程序进行备份,就像我在案例中所做的那样。

另外,链接:https://help.ubuntu.com/stable/serverguide/postgresql.html帮助设置加密密码并设置md5以验证postgres用户。

一切都完成后,检查终端中运行的postgres server版本:

sudo -u postgres psql postgres

输入密码后在postgres终端运行:

SHOW SERVER_VERSION;

它将输出如下内容:

 server_version 
----------------
 9.4.5

为了设置和启动postgres,我使用了命令:

> sudo bash # root
> su postgres # postgres

> /etc/init.d/postgresql start
> /etc/init.d/postgresql stop

然后从文件中恢复数据库:

> psql -f /home/ubuntu_username/Backup_93.sql postgres

或者,如果不起作用尝试这个:

> pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d name_of_database ~/your_file.dump

如果您使用Rails,请在拉动代码后执行bundle exec rake db:migrate :)


0
投票

我认为这是您将postgres更新为9.6的解决方案的最佳链接

https://sandymadaan.wordpress.com/2017/02/21/upgrade-postgresql9-3-9-6-in-ubuntu-retaining-the-databases/

0
投票

对于Mac通过自制软件:

brew tap petere/postgresql

brew install <formula>(例如:brew install petere/postgresql/postgresql-9.6

删除旧Postgres:

brew unlink postgresql

brew link -f postgresql-9.6

如果发生任何错误,请不要忘记在每个步骤中阅读并遵循brew说明。

请查看更多信息:https://github.com/petere/homebrew-postgresql


0
投票

在Windows 10上,因为我有npm,我安装了rimraf包。 npm install rimraf -g

使用命令pg_dump -U $username --format=c --file=$mydatabase.sqlc $dbname逐个备份所有数据库

然后安装了最新的PostgreSQL版本,即11.2,这次促使我使用端口5433。

其次是卸载旧版本的PostgreSQL我的10.注意卸载程序可能会发出不删除文件夹C:\PostgreSQL\10\data的警告。这就是为什么我们使用rimraf永久删除文件夹及其子文件夹的下一步。

切换到PostgreSQL安装目录并运行命令rimraf 10。 10是目录名。注意使用旧版本的PostgreSQL,即9.5或其他东西。

现在将C:\PostgreSQL\pg11\bin, C:\PostgreSQL\pg11\lib添加到Windows环境变量中。注意我的新安装版本是11,因此我使用pg11

导航到qazxsw poi然后打开qazxsw poi编辑qazxsw poi到qazxsw poi

而已。打开cmd并输入C:\PostgreSQL\data\pg11

您现在可以使用命令postgresql.conf逐个恢复所有支持的数据库


53
投票

这是Ubuntu用户的解决方案

首先,我们必须停止postgresql

sudo /etc/init.d/postgresql stop

创建一个名为/etc/apt/sources.list.d/pgdg.list的新文件并添加到下面的行

deb http://apt.postgresql.org/pub/repos/apt/ utopic-pgdg main

按照以下命令

wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.4
sudo pg_dropcluster --stop 9.4 main 
sudo /etc/init.d/postgresql start

现在我们拥有一切,只需要升级如下

sudo pg_upgradecluster 9.3 main
sudo pg_dropcluster 9.3 main

而已。大多数升级的群集将在端口号5433上运行。使用以下命令检查它

sudo pg_lsclusters

49
投票

尽管上面的所有答案,这里我的5美分。

它适用于任何操作系统以及任何对任何postgres版本。

  • 停止任何正在运行的postgres实例;
  • 安装新版本并启动它;检查您是否也可以连接到新版本;
  • 将旧版本的postgresql.conf - > port5432更改为5433;
  • 启动旧版postgres实例;
  • 打开终端和CD到新版本bin文件夹;
  • 运行pg_dumpall -p 5433 -U <username> | psql -p 5432 -U <username>
  • 停止旧的postgres运行实例;

11
投票

user manual深入介绍了这一主题。您可以:

  • pg_upgrade就地;要么
  • pg_dumppg_restore

如果有疑问,请使用转储。不要删除旧的数据目录,只要保留它以防出现问题/你犯了错误;这样你就可以回到你未改变的9.3安装。

有关详细信息,请参阅手册。

如果你遇到困难,请发一个详细的问题,解释你是如何被困,在哪里以及你先尝试过的。这有点依赖于你如何安装PostgreSQL,因为有几种不同的PostgreSQL for OS X“发行版”(不幸的是)。所以你需要提供这些信息。


10
投票

更新:此过程与升级9.6到10相同;只需修改命令以反映版本9.610,其中9.6是旧版本,10是新版本。请务必相应地调整“旧”和“新”目录。


我刚刚在Ubuntu上将PostgreSQL 9.5升级到9.6并且认为我会分享我的发现,因为有几个OS /特定于程序包的细微差别需要注意。

(我不想手动转储和恢复数据,因此这里的其他几个答案都不可行。)

简而言之,该过程包括安装新版本的PostgreSQL以及旧版本(例如,9.5和9.6),然后运行pg_upgrade二进制文件,这在https://www.postgresql.org/docs/9.6/static/pgupgrade.html的(某些)细节中有解释。

pg_upgrade唯一“棘手”的方面是,在执行命令之前未能为参数传递正确的值,或者未能以正确的用户或cd登录到正确的位置,可能会导致隐藏的错误消息。

在Ubuntu(可能是Debian)上,如果您使用的是“官方”仓库deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main,并且如果您没有更改默认文件系统路径或运行时选项,则以下过程应该可以完成。

安装新版本(请注意我们明确指定了9.6):

sudo apt install postgresql-9.6

安装成功后,两个版本将并行运行,但在不同的端口上运行。安装输出在底部提到了这一点,但很容易忽略:

Creating new cluster 9.6/main ...
  config /etc/postgresql/9.6/main
  data   /var/lib/postgresql/9.6/main
  locale en_US.UTF-8
  socket /var/run/postgresql
  port   5433

停止两个服务器实例(这将同时停止):

sudo systemctl stop postgresql

切换到专用的PostgreSQL系统用户:

su postgres

进入他的主目录(不这样做会导致错误):

cd ~

pg_upgrade需要以下输入(pg_upgrade --help告诉我们这个):

When you run pg_upgrade, you must provide the following information:
  the data directory for the old cluster  (-d DATADIR)
  the data directory for the new cluster  (-D DATADIR)
  the "bin" directory for the old version (-b BINDIR)
  the "bin" directory for the new version (-B BINDIR)

可以使用“长名称”指定这些输入,以便更容易可视化:

  -b, --old-bindir=BINDIR       old cluster executable directory
  -B, --new-bindir=BINDIR       new cluster executable directory
  -d, --old-datadir=DATADIR     old cluster data directory
  -D, --new-datadir=DATADIR     new cluster data directory

我们还必须通过--new-options开关,因为如果不这样做会导致以下结果:

connection to database failed: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/lib/postgresql/.s.PGSQL.50432"?

发生这种情况是因为在没有此开关的情况下应用默认配置选项,这会导致使用不正确的连接选项,从而导致套接字错误。

从新的PostgreSQL版本执行pg_upgrade命令:

/usr/lib/postgresql/9.6/bin/pg_upgrade --old-bindir=/usr/lib/postgresql/9.5/bin --new-bindir=/usr/lib/postgresql/9.6/bin --old-datadir=/var/lib/postgresql/9.5/main --new-datadir=/var/lib/postgresql/9.6/main --old-options=-cconfig_file=/etc/postgresql/9.5/main/postgresql.conf --new-options=-cconfig_file=/etc/postgresql/9.6/main/postgresql.conf

注销专用系统用户帐户:

exit

升级现在已经完成,但是,新实例将绑定到端口5433(标准默认值为5432),因此如果尝试在“切换”之前测试新实例,请记住这一点。

正常启动服务器(同样,这将启动旧实例和新实例):

systemctl start postgresql

如果要将新版本设置为默认版本,则需要编辑有效的配置文件,例如/etc/postgresql/9.6/main/postgresql.conf,并确保端口定义如下:

port = 5432

如果这样做,要么同时将旧版本的端口号更改为5433(在启动服务之前),或者只是删除旧版本(这不会删除您的实际数据库内容;您需要使用apt --purge remove postgresql-9.5即将发生):

apt remove postgresql-9.5

上面的命令将停止所有实例,因此您需要最后一次启动新实例:

systemctl start postgresql

作为最后一点,请不要忘记考虑pg_upgrade的好建议:

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

9
投票

如果您使用的是自制和自制服务,您可以这样做:

brew services stop postgresql
brew upgrade postgresql
brew postgresql-upgrade-database
brew services start postgresql

我认为如果您使用高级postgres功能,这可能无法完全发挥作用,但它对我来说非常有用。


8
投票

站在其他可怜的生物的肩膀上,我已经能够按照这些步骤在升级到约塞米蒂后恢复运行:

假设您已使用home-brew安装和升级Postgres,您可以执行以下步骤。

  1. 停止当前的Postgres服务器: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  2. 初始化一个新的9.4数据库: initdb /usr/local/var/postgres9.4 -E utf8
  3. 安装postgres 9.3(因为它不再出现在我的机器上): brew install homebrew/versions/postgresql93
  4. 添加Yosemite升级期间删除的目录: mkdir -p /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/touch /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep
  5. 运行pg_upgradepg_upgrade -v -d /usr/local/var/postgres -D /usr/local/var/postgres9.4 -b /usr/local/Cellar/postgresql93/9.3.5/bin/ -B /usr/local/Cellar/postgresql/9.4.0/bin/
  6. 将新数据移动到位: cd /usr/local/var mv postgres postgres9.3 mv postgres9.4 postgres
  7. 重新启动Postgres: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  8. 检查/usr/local/var/postgres/server.log以获取详细信息并确保新服务器正常启动。
  9. 最后,重新安装相关的库? pip install --upgrade psycopg2 gem uninstall pg gem install pg

4
投票

看起来这个解决方案现已成为Homebrew:

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

1
投票

这样做对我来说。

https://gist.github.com/dideler/60c9ce184198666e5ab4

精炼到位。老实说,我不打算理解PostgreSQL的内容,我想完成这些工作。

© www.soinside.com 2019 - 2024. All rights reserved.