如何使用sql或phpPgAdmin更改PostgreSQL数据库的数据库编码? [英] How can I change database encoding for a PostgreSQL database using sql or phpPgAdmin?

查看:238
本文介绍了如何使用sql或phpPgAdmin更改PostgreSQL数据库的数据库编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用sql或phpPgAdmin更改PostgreSQL数据库的数据库编码?

How can i change database encoding for a PostgreSQL database using sql or phpPgAdmin?

推荐答案

总之,您无法执行此操作仅使用phpPgAdmin或SQL,而不会冒现有数据损坏的风险。
您必须导出所有数据,使用正确的编码创建数据库并恢复导出的数据。

In short, you cannot do this with only phpPgAdmin or SQL without risking existing data corruption. You have to export all data, create database with correct encoding and restore exported data.

这是应该进行的操作:


  1. 创建数据库转储

pg_dump your_database> your_database.sql

这将以您当前使用的编码格式将数据库保存为sql格式。

this will save your database in sql format, in encoding you currently have.

删除数据库(或重命名):

DROP DATABASE your_database

如果您有足够的存储空间来做,我建议您保留旧数据库,直到您确定新文件都可以了,重命名

if you have enough storage to do it I recommend leaving old database until you make sure everything is OK with new one, rename it:

ALTER DATABASE your_database重命名为your_database_backup;

使用新编码创建数据库

使用编码 UNICODE模板创建数据库your_database = template0;

从之前创建的转储中导入数据

PGCLIENTENCODING = YOUR_OLD_ENCODING psql -f your_database.sql your_database

您需要设置psql客户端

you need to set psql client encoding to one you had in old database.

更改编码即时访问是不可能的,因为这将需要重写大多数内部数据库数据,这几乎等于重新创建我描述的db方法。

Changing encoding on-the-fly isn't possible as it would require rewriting most of internal database data which is almost equal to recreating db way I described.

只需更改有关数据库的内部postgres信息,更改后的所有新数据都将正确保存,但是您现有的数据可能会损坏

It is possible to just alter internal postgres informations about database and any new data after this alteration will be saved correctly, however your existing data might get corrupted.

这篇关于如何使用sql或phpPgAdmin更改PostgreSQL数据库的数据库编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆