我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
我正在为我的Ruby on Rails应用程序(在Mac OS X 10.9上)使用PostgreSQL数据库。
关于如何升级PostgreSQL数据库有详细的说明吗?
我担心我会破坏数据库中的数据或把它弄乱。
当前回答
如果你正在使用自制和自制服务,你可能只需要做:
brew services stop postgresql
brew upgrade postgresql
brew postgresql-upgrade-database
brew services start postgresql
我认为如果您正在使用高级postgres功能,这可能不完全有效,但它对我来说非常有效。
其他回答
看来Homebrew现在已经有了解决方案:
$ brew info postgresql
...
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
....
Mac通过homebrew:
编译petere/postgresql,
(例如:Brew install peere /postgresql/postgresql-9.6)
删除旧Postgres:
Brew unlink postgresql
编译链接-f postgresql-9.6
如果发生任何错误,不要忘记阅读并遵循每一步的酿造说明。
查看更多信息:https://github.com/petere/homebrew-postgresql
我的解决方案是将这两种资源结合起来:
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
我在Windows 10上从Postgresql 11升级到Postgresql 12的解决方案如下。
首先,你需要能够停止和启动Postgresql服务。您可以在Powershell中通过以下命令完成此操作。
开始: pg_ctl start -D " d:\postgresql\11\data "
站: pg_ctl stop -D " d:\postgresql\11\data "
状态: pg_ctl status -D " d:\postgresql\11\data "
在进行升级之前进行备份是明智的。Postgresql 11实例必须正在运行。然后复制全局函数
pg_dumpall -U postgres -g -f d:\bakup\postgresql\11\globals.sql
然后是每个数据库
pg_dump -U postgres -Fc <database> > d:\backup\postgresql\11\<database>.fc
or
pg_dump -U postgress -Fc -d <database> -f d:\backup\postgresql\11\<database>.fc . d:\backup\postgresql\11\<database>.fc . d:\backup\postgresql\11\
如果还没有安装Postgresql 12 (Postgresql 11也安装在5433端口上)
然后按如下步骤进行升级:
1)停止Postgresql 11服务(见上文)
2)编辑d:\postgresql\12\data目录下的postgresql.conf文件,将port = 5433修改为port = 5432
3)编辑windows用户环境路径(windows start然后输入env)指向Postgresql 12而不是Postresql 11
4)执行upgrade,输入如下命令。
pg_upgrade `
-b “c:\program files\postgresql\11\bin” `
-B “c:\program files\postgresql\12\bin” `
-d “d:\postgresql\11\data” `
-D “d:\postgresql\12\data” --username=postgres
(在powershell中使用反引号(或反引号)'来继续下一行的命令)
5)最后启动新的Postgresql 12服务
pg_ctl start -D " d:\postgresql\12\data "