我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
当前回答
站在其他可怜的动物的肩膀上,踩着淤泥,我能够按照以下步骤重新站起来,并在约塞米蒂升级后运行:
假设您已经使用自制程序安装和升级Postgres,您可以执行以下步骤。
Stop current Postgres server: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Initialize a new 9.4 database: initdb /usr/local/var/postgres9.4 -E utf8 Install postgres 9.3 (as it was no longer present on my machine): brew install homebrew/versions/postgresql93 Add directories removed during Yosemite upgrade: 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 run 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/ Move new data into place: cd /usr/local/var mv postgres postgres9.3 mv postgres9.4 postgres Restart Postgres: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly. Finally, re-install related libraries? pip install --upgrade psycopg2 gem uninstall pg gem install pg
其他回答
更新:从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
在Windows 10上,因为我有npm,所以我安装了rimraf包。NPM安装rimraf
使用pg_dump -U $username——format=c——file=$mydatabase命令逐个备份所有数据库。美元sqlc dbname
然后安装了最新的PostgreSQL版本,即11.2,这提示我这次使用端口5433。
其次是卸载旧版本的PostgreSQL我是10。注意,卸载程序可能会提示不删除文件夹C:\PostgreSQL\10\data。这就是为什么我们有下一步使用rimraf永久删除文件夹和它的子文件夹。
2 .进入PostgreSQL安装目录,执行命令rimraf 10。10为目录名。注意使用旧版本的PostgreSQL,比如9.5之类的。
现在将C:\PostgreSQL\pg11\bin, C:\PostgreSQL\pg11\lib添加到Windows环境变量中。注意我的新安装版本是11,这就是为什么我使用pg11。
打开C:\PostgreSQL\data\pg11,打开PostgreSQL .conf编辑port = 5433到port = 5432
就是这样。打开cmd,输入psql -U postgres
现在,您可以使用命令pg_restore -U $username——dbname=$databasename $filename来逐个恢复所有已备份的数据库
站在其他可怜的动物的肩膀上,踩着淤泥,我能够按照以下步骤重新站起来,并在约塞米蒂升级后运行:
假设您已经使用自制程序安装和升级Postgres,您可以执行以下步骤。
Stop current Postgres server: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Initialize a new 9.4 database: initdb /usr/local/var/postgres9.4 -E utf8 Install postgres 9.3 (as it was no longer present on my machine): brew install homebrew/versions/postgresql93 Add directories removed during Yosemite upgrade: 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 run 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/ Move new data into place: cd /usr/local/var mv postgres postgres9.3 mv postgres9.4 postgres Restart Postgres: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly. Finally, re-install related libraries? pip install --upgrade psycopg2 gem uninstall pg gem install pg
这对我很有用。
https://gist.github.com/dideler/60c9ce184198666e5ab4
言简意赅。老实说,我的目标不是理解PostgreSQL的精髓,我只想把事情做好。
假设您已经使用自制程序安装和升级Postgres,您可以执行以下步骤。
Stop current Postgres server: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Initialize a new 10.1 database: initdb /usr/local/var/postgres10.1 -E utf8 run pg_upgrade (note: change bin version if you're upgrading from something other than below): 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/ -v to enable verbose internal logging -d the old database cluster configuration directory -D the new database cluster configuration directory -b the old PostgreSQL executable directory -B the new PostgreSQL executable directory Move new data into place: cd /usr/local/var mv postgres postgres9.6 mv postgres10.1 postgres Restart Postgres: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Check /usr/local/var/postgres/server.log for details and to make sure the new server started properly. Finally, re-install the rails pg gem gem uninstall pg gem install pg
我建议你花点时间阅读PostgreSQL文档,了解你在上述步骤中所做的事情,以减少挫折。