首页 > 解决方案 > 将 Postgres 从 10 升级到 12——编码问题

问题描述

我正在尝试从 10 12 升级 postgres 服务器,并且遇到编码问题。我正在遵循我认为已建立的食谱。

看哪:

postgres@serverbot:~$ psql -l
                              List of databases
    Name     |  Owner   | Encoding  | Collate | Ctype |   Access privileges
-------------+----------+-----------+---------+-------+-----------------------
 postgres    | postgres | SQL_ASCII | C       | C     |
 template0   | postgres | SQL_ASCII | C       | C     | =c/postgres          +
             |          |           |         |       | postgres=CTc/postgres
 template1   | postgres | SQL_ASCII | C       | C     | postgres=CTc/postgres+
             |          |           |         |       | =c/postgres
 thingsboard | postgres | SQL_ASCII | C       | C     | =Tc/postgres         +
             |          |           |         |       | postgres=CTc/postgres+
             |          |           |         |       | nagios=c/postgres
(4 rows)

注意 10 数据库上的编码。是时候为 12 创建数据库了。

postgres@serverbot:~$ sudo service postgresql stop

postgres@serverbot:~$ /usr/lib/postgresql/12/bin/initdb -E SQL_ASCII --locale=C  -D /var/lib/postgresql/12/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/lib/postgresql/12/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/New_York
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/lib/postgresql/12/bin/pg_ctl -D /var/lib/postgresql/12/data -l logfile start

优秀的!让我们启动新服务器。

postgres@serverbot:~$ /usr/lib/postgresql/12/bin/pg_ctl -D /var/lib/postgresql/12/data -l logfile start
waiting for server to start.... done
server started

并验证编码...

postgres@serverbot:~$ psql -l
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     |
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
(3 rows)

一切都匹配...是时候升级了!

postgres@serverbot:~$ /usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/10/main \
--new-datadir=/var/lib/postgresql/12/main \
--old-bindir=/usr/lib/postgresql/10/bin \
--new-bindir=/usr/lib/postgresql/12/bin \
--old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' \
--link --check
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok

encodings for database "postgres" do not match:  old "SQL_ASCII", new "UTF8"
Failure, exiting

postgres@serverbot:~$

嗬!

这里有什么问题?我断言编码确实匹配,但我被卡住了。

任何人都可以提供任何建议吗?

标签: postgresql

解决方案


我看到的是:

postgres@serverbot:~$ /usr/lib/postgresql/12/bin/pg_ctl -D /var/lib/postgresql/12/data -l logfile start
waiting for server to start.... done
server started

进而:

postgres@serverbot:~$ /usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/10/main \
--new-datadir=/var/lib/postgresql/12/main \

[...]

注意$PGDATA目录。您执行的数据库集群initdb与您正在执行的数据库集群不同pg_upgrade

更新。由于您似乎使用的是基于 Debian 的操作系统并且它是 Postgres 打包,因此坚持使用打包工具可能会更好:

sudo pg_createcluster  --locale=C 12 ascii 
Creating new PostgreSQL cluster 12/ascii ...
/usr/lib/postgresql/12/bin/initdb -D /var/lib/postgresql/12/ascii --auth-local peer --auth-host md5 --locale C
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/12/ascii ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Los_Angeles
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:


    pg_ctlcluster 12 ascii start

Ver Cluster Port Status Owner    Data directory               Log file
12  ascii   5434 down   postgres /var/lib/postgresql/12/ascii /var/log/postgresql/postgresql-12-ascii.log


postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
(3 rows)

我也会使用pg_upgradecluster,请参阅此处了解更多信息。这使所有内容都在同一个系统中。


推荐阅读