gpt4 book ai didi

postgresql - 将 postgres 表主键 UUID 更改为字符变化

转载 作者:行者123 更新时间:2023-11-29 13:13:41 27 4
gpt4 key购买 nike

我想将我的 PostgreSQL 表主键 UUID 转换为字符变化

ALTER TABLE payment_authorization ALTER COLUMN id TYPE VARCHAR;

当我运行上面的命令时显示以下错误,因为外键约束失败。在我的系统中有 200 个表。有什么简单的方法可以更改所有表的主键吗?

最佳答案

更改所有表可能会很慢而且很麻烦。

最简单的解决方案可能是:

  • 导出数据库

    pg_dump -F p -f dumpfile.sql dbname
  • 使用编辑器将转储中的 uuid 替换为 text:

    sed --in-place -e 's/uuid/text/g' dumpfile.sql
  • 删除并重新创建数据库:

    DROP DATABASE dbname;
    CREATE DATABASE dbname;
  • 导入转储:

    psql -U postgres -d dbname -1 -f dumpfile.sql

关于postgresql - 将 postgres 表主键 UUID 更改为字符变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720888/

27 4 0
文章推荐: PostgresQL 错误 : relation doesn't exist