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

关于如何升级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后拉代码:)

其他回答

下面是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

尽管上面有所有的答案,我还是有5美分。

它适用于任何操作系统和从任何到任何postgres版本。

停止任何正在运行的postgres实例; 安装新版本并启动它;检查你是否可以连接到新版本; 将旧版本postgresql.conf ->端口从5432修改为5433; 启动旧版postgres实例; 打开终端并cd到新版本的bin文件夹; 执行命令pg_dumpall -p 5433 -U <username> | psql -p 5432 -U <username> 停止旧的postgres运行实例;

在Windows上,当尝试使用pg_upgrade时,我一直面临不同的错误消息。

为我节省了很多时间

备份数据库 卸载PostgreSQL的所有副本 安装9.5 恢复数据库

假设您已经使用自制程序安装和升级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文档,了解你在上述步骤中所做的事情,以减少挫折。

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

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后拉代码:)