[英]alter table in multiple database postgres
I have multiple database, each has table "authentication". 我有多个数据库,每个都有表“身份验证”。 In each table I want to drop a constraint and replace it with a new one.
在每个表中,我想删除一个约束并用一个约束替换它。 That would be great if I had not to do that manually.
如果我不想手动完成那将是很好的。
ALTER TABLE authentication DROP CONSTRAINT uk_authentication_01;
ALTER TABLE authentication ADD CONSTRAINT uk_authentication_01 UNIQUE (authenticator, method);
is there a way to do a bulk alter with a script? 有没有办法用脚本进行批量修改?
You could iterate over your databases in a shell script: 您可以在shell脚本中迭代数据库:
for db in dbname1 dbname2 dbname3...
do
psql -d $db -U username << EOF
ALTER TABLE authentication DROP CONSTRAINT uk_authentication_01;
ALTER TABLE authentication ADD CONSTRAINT uk_authentication_01 UNIQUE (authenticator, method);
EOF
done
I assume that all databases are on the same server? 我假设所有数据库都在同一台服务器上? If that is true you can simply do the SELECT:
如果这是真的,你可以简单地做SELECT:
SELECT 'SELECT * FROM dblink_exec(''dbname=' || datname
|| '''::text, ''ALTER TABLE authentication
DROP CONSTRAINT uk_authentication_01;
ALTER TABLE authentication ADD CONSTRAINT uk_authentication_01
UNIQUE (authenticator, method);''::text);'
FROM pg_database WHERE datistemplate = false;
Then copy the result of this query and run it. 然后复制此查询的结果并运行它。
And you'll need for that dblink extension: 你需要这个dblink扩展:
CREATE EXTENSION dblink;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.