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

关于如何升级PostgreSQL数据库有详细的说明吗?

我担心我会破坏数据库中的数据或把它弄乱。


当前回答

在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来逐个恢复所有已备份的数据库

其他回答

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

在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来逐个恢复所有已备份的数据库

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

更新:从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上,当尝试使用pg_upgrade时,我一直面临不同的错误消息。

为我节省了很多时间

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