我有多个不同名称的 PostgreSQL-15 数据库来满足我们的业务运营数据,例如:不同用途的试点、生产和存档数据。
我正在尝试编写一个 shell 脚本来检查不同数据库下每个表的所有者是否有自己的表名。例如:
select tableowner from pg_catalog.pg_tables where schemaname = 'public' AND tablename = '$1' AND pg_database.datname = '$2';
我的 postgresql 响应为:
ERROR: missing FROM-clause entry for table "pg_database"
我试图检索 pg_catalog.pg_tables 属性,发现属性中没有保存数据库信息。
我的问题是:如何通过在 shell 脚本中的单个 psql 语句中给出表名和数据库名来查询表属性,例如:
tableowner=`psql -h $prim_host -p $port -U postgres -t -c "select tableowner from pg_catalog.pg_tables where schemaname = 'public' AND tablename = '$1' AND pg_database.datname = '$2';"`
请指教!
您必须连接到包含该表的数据库: