移动 PostgreSQL 数据库在非 ascii 字符上失败,并显示“值太长”;

发布于 2024-08-13 19:52:45 字数 947 浏览 2 评论 0原文

# Dump my database to a tar file
pg_dump -f myDatabase.tar -F t -h myServer -U myUser -W -i myDatabase
# create a new database
createdb -h myServer -U myUser -T template0 myDatabaseCopy
# restore my database
pg_restore -d myDatabaseCopy -h myServer -U myUser myDatabase.tar

然后我收到此错误,整个表的导入失败。

psql:/home/me/myDatabase.tar:660266: 错误: 值对于类型字符变化来说太长 (100) 上下文:复制 myTable,第 591 行,myColumn 栏:“国务院‘伊拉克的未来’项目的前成员,现在在大西洋海岸......”

这些帽子是那些烦人的卷曲单引号和双引号。在我看来,它们一开始适合该列,但在导出/导入过程中的某个地方它们会扩展,然后不再适合字符变化(100)列。

实际上,我正在将实时数据库移动到我几乎没有权限的服务器上,因此仅 sql 的解决方案会很棒。有没有办法做类似

UPDATE myTable SET myColumn = removeNonAscii(myColumn) WHERE hasNonAscii(myColumn)

编辑的事情: 哈贝明白了。我改变

createdb -h myServer -U myUser -T template0 myDatabaseCopy

createdb -h myServer -U myUser -T template0 -E UTF8 myDatabaseCopy

,这就成功了。

# Dump my database to a tar file
pg_dump -f myDatabase.tar -F t -h myServer -U myUser -W -i myDatabase
# create a new database
createdb -h myServer -U myUser -T template0 myDatabaseCopy
# restore my database
pg_restore -d myDatabaseCopy -h myServer -U myUser myDatabase.tar

Then I get this error, and the import fails for an entire table.

psql:/home/me/myDatabase.tar:660266: ERROR: value too long for type character varying(100)
CONTEXT: COPY myTable, line 591, column myColumn: "A former member of the State Departmentâs âFuture of Iraqâ project and now at the Atlantic Cou..."

Those hat-a's are those annoying curly single and double quotes. It looks to me like they fit in the column at first, but somewhere in the export / import process they expand, and then no longer fit in the character varying(100) column.

I'm actually moving a live database on a server I have little permission for, so a sql only solution would be great. Is there a way to do something like

UPDATE myTable SET myColumn = removeNonAscii(myColumn) WHERE hasNonAscii(myColumn)

EDIT:
habe got it. I changed

createdb -h myServer -U myUser -T template0 myDatabaseCopy

to

createdb -h myServer -U myUser -T template0 -E UTF8 myDatabaseCopy

and that did the trick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-08-20 19:52:45

看来问题是由数据库编码引起的。
例如,源数据库采用“UTF-8”编码,目标数据库采用更受限制的字符集(例如“SQL_ASCII”)。

检查两个数据库的编码(psql 实用程序的 \l 有所帮助)。
如果它们不同,请使用 -Exxx 选项重新创建目标数据库。

It seems that the problem is caused by database encoding.
For example, source database is encoded in “UTF-8” and destination is in more restricted character set like “SQL_ASCII.”

Check the encodings of both databases (\l of psql utility helps).
If they differ, recreate destination database with -Exxx option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文