我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
当前回答
更新:从9.5升级到至少11.5,这个过程是一样的;只需修改命令以反映版本9.6和10,其中9.6是旧版本,10是新版本。一定要相应地调整“旧”和“新”目录。
我刚刚在Ubuntu上将PostgreSQL 9.5升级到9.6,我想分享一下我的发现,因为有一些操作系统/包特定的细微差别需要注意。
(我不想手动转储和恢复数据,所以这里的其他几个答案都不可行。)
简而言之,这个过程包括在旧版本(例如,9.5和9.6)的同时安装新版本的PostgreSQL,然后运行pg_upgrade二进制文件,这在https://www.postgresql.org/docs/9.6/static/pgupgrade.html有(一些)详细解释。
pg_upgrade的唯一“棘手”之处在于未能为参数传递正确的值,或者未能在执行命令之前以正确的用户或cd身份登录到正确的位置,这可能会导致神秘的错误消息。
在Ubuntu(可能还有Debian)上,如果你使用的是“官方的”repo, 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
其他回答
这对我很有用。
https://gist.github.com/dideler/60c9ce184198666e5ab4
言简意赅。老实说,我的目标不是理解PostgreSQL的精髓,我只想把事情做好。
下面是Ubuntu用户的解决方案
首先,我们必须停止postgresql
sudo /etc/init.d/postgresql stop
创建一个名为/etc/apt/sources.list.d/pgdg的新文件。列出并在行下添加
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
在Windows上,当尝试使用pg_upgrade时,我一直面临不同的错误消息。
为我节省了很多时间
备份数据库 卸载PostgreSQL的所有副本 安装9.5 恢复数据库
我的解决方案是将这两种资源结合起来:
https://gist.github.com/tamoyal/2ea1fcdf99c819b4e07d
and
http://www.gab.lc/articles/migration_postgresql_9-3_to_9-4
第二个比第一个更有帮助。也要不要,不要按照步骤去做,因为有些是没有必要的。 此外,如果您不能通过postgres控制台备份数据,您可以使用替代方法,使用pgAdmin 3或其他程序进行备份,就像我在我的案例中所做的那样。
还有,链接:https://help.ubuntu.com/stable/serverguide/postgresql.html 帮助设置postgres用户的加密密码和md5认证。
所有这些都完成后,检查终端中运行的postgres服务器版本:
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后拉代码:)
更新:从9.5升级到至少11.5,这个过程是一样的;只需修改命令以反映版本9.6和10,其中9.6是旧版本,10是新版本。一定要相应地调整“旧”和“新”目录。
我刚刚在Ubuntu上将PostgreSQL 9.5升级到9.6,我想分享一下我的发现,因为有一些操作系统/包特定的细微差别需要注意。
(我不想手动转储和恢复数据,所以这里的其他几个答案都不可行。)
简而言之,这个过程包括在旧版本(例如,9.5和9.6)的同时安装新版本的PostgreSQL,然后运行pg_upgrade二进制文件,这在https://www.postgresql.org/docs/9.6/static/pgupgrade.html有(一些)详细解释。
pg_upgrade的唯一“棘手”之处在于未能为参数传递正确的值,或者未能在执行命令之前以正确的用户或cd身份登录到正确的位置,这可能会导致神秘的错误消息。
在Ubuntu(可能还有Debian)上,如果你使用的是“官方的”repo, 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