Мы используем расширение postgres_fdw для доступа к представлениям другой базы данных. Я проверил доступ к чужим таблицам на 2 разных PostgreSQL серверах, он работает на одном сервере, а другой сервер выдает ошибку ниже
SQL Error [2F003]: ERROR: password is required
Detail: Non-superuser cannot connect if the server does not request a password.
Hint: Target server's authentication method must be changed.
Я проверил файл pg_hba.conf, и один сервер использует метод доверия для локальный доступ и все еще не получает ошибку, однако другой сервер получает ошибку с доверием и md5 обе опции.
ниже приведены файлы conf обоих серверов
- Сервер, который выдает ошибку
сервер, на котором запрос выполняется без ошибок
Ниже приведен мой скрипт для создания стороннего сервера
DO $$
Begin
if not exists(select 1 from pg_foreign_server where srvname='fdw_dxpcore') then
If not Exists (select * from pg_user where usename='pgfwduser') then
create role pgfwduser with login password 'test@123';
end if;
If Exists (select * from pg_user where usename='pgfwduser') then
IF Exists (select * from pg_database where datname = 'ars') Then
grant connect on database ars to pgfwduser;
End IF;
grant usage on schema public to pgfwduser;
GRANT SELECT ON all tables in schema public TO pgfwduser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES to pgfwduser;
End IF;
-- create foreign data wrapper extension
CREATE EXTENSION if not exists postgres_fdw;
-- create foreign servers for dxpcore
if not exists(select 1 from pg_foreign_server where srvname='fdw_dxpcore') then
CREATE SERVER fdw_dxpcore
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', port '5432', dbname 'dxpcore', fetch_size '50000');
end if;
--- assign foreign table access to
if exists (select 1 from pg_roles where rolname='postgres') then
if not exists (select * from information_schema.user_mappings where foreign_server_name = 'fdw_dxpcore' and authorization_identifier = 'postgres') Then
CREATE USER MAPPING FOR postgres
SERVER fdw_dxpcore
OPTIONS (user 'pgfwduser',password 'test@123');
End IF;
end if;
/*Import Foreign Tables*/
IMPORT FOREIGN SCHEMA public LIMIT TO (v_resguestids,v_resguestids_shore_ship)
FROM SERVER fdw_dxpcore INTO public;
if exists (select 1 from pg_roles where rolname='pgappuser') then
if not exists (select * from information_schema.user_mappings where foreign_server_name = 'fdw_dxpcore' and authorization_identifier = 'pgappuser') Then
CREATE USER MAPPING FOR pgappuser
SERVER fdw_dxpcore
OPTIONS (user 'pgfwduser',password 'test@123');
End IF;
end if;
End if;
If Exists (select * from pg_user where usename='pgappuser') Then
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES to pgappuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, USAGE ON SEQUENCES to pgappuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS to pgappuser;
GRANT USAGE ON SCHEMA public TO pgappuser;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO pgappuser;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO pgappuser;
GRANT EXECUTE ON ALL FUNCTIONs IN SCHEMA public TO pgappuser;
End IF;
If Exists (select * from pg_user where usename='pgfwduser') Then
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES to pgfwduser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, USAGE ON SEQUENCES to pgfwduser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS to pgfwduser;
GRANT USAGE ON SCHEMA public TO pgfwduser;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO pgfwduser;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO pgfwduser;
GRANT EXECUTE ON ALL FUNCTIONs IN SCHEMA public TO pgfwduser;
End IF;
End $$;