我正在将PostgreSQL数据库用于我的Ruby on Rails应用程序(在Mac OS X 10.9上).
有关如何升级PostgreSQL数据库的详细说明吗?
我担心我会破坏数据库中的数据或弄乱它.
这是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
尽管上面的所有答案,这里我的5美分.
它适用于任何操作系统以及任何对任何postgres版本.
停止任何正在运行的postgres实例;
安装新版本并启动它; 检查您是否也可以连接到新版本;
改变旧的版本的postgresql.conf
> - port
从5432
到5433
;
启动旧版postgres实例;
打开终端和CD到新版本cd
文件夹;
跑 bin
停止旧的postgres运行实例;
该用户手册涵盖了深入这个话题.您可以:
pg_upgrade
到位; 要么
pg_dump
和pg_restore
.
如果有疑问,请使用转储.不要删除旧的数据目录,只要保留它以防出现问题/你犯了错误; 这样你就可以回到你未改变的9.3安装.
有关详细信息,请参阅手册.
如果你遇到困难,请发一个详细的问题,解释你是如何被困,在哪里以及你先尝试过的.这有点依赖于你如何安装PostgreSQL,因为有几种不同的PostgreSQL for OS X"发行版"(不幸的是).所以你需要提供这些信息.
站在其他可怜的生物的肩膀上,我已经能够按照这些步骤在升级到约塞米蒂后恢复运行:
假设您已使用home-brew安装和升级Postgres,您可以执行以下步骤.
停止当前的Postgres服务器:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
初始化一个新的9.4数据库:
initdb /usr/local/var/postgres9.4 -E utf8
安装postgres 9.3(因为它不再出现在我的机器上):
brew install homebrew/versions/postgresql93
添加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
运行pg_upgrade
:
pg_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/
将新数据移动到位:
cd /usr/local/var mv postgres postgres9.3 mv postgres9.4 postgres
重新启动Postgres:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
检查/usr/local/var/postgres/server.log
详细信息并确保新服务器正常启动.
最后,重新安装相关的库?
pip install --upgrade psycopg2 gem uninstall pg gem install pg
假设您已使用home-brew安装和升级Postgres,您可以执行以下步骤.
停止当前的Postgres服务器:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
初始化一个新的10.1数据库:
initdb /usr/local/var/postgres10.1 -E utf8
运行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/
将新数据移动到位:
cd /usr/local/var mv postgres postgres9.6 mv postgres10.1 postgres
重新启动Postgres:
-v
检查-d
详细信息并确保新服务器正常启动.
最后,重新安装rails -D
gem
gem uninstall pg gem install pg
我建议您花一些时间阅读PostgreSQL文档,以准确了解您在上述步骤中正在做的事情,以尽量减少挫折.
更新:此过程与升级9.6到10相同; 只需修改,以反映版本的命令9.6
和10
,其中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
如果您使用的是自制和自制服务,您可以这样做:
brew services stop postgresql brew upgrade postgresql brew postgresql-upgrade-database brew services start postgresql
我认为如果您使用高级postgres功能,这可能无法完全发挥作用,但它对我来说非常有用.