We are using postgres_fdw extension to access views of another database. I have tested accessing foreign tables on 2 different PostgreSQL servers, it's working on one server and another server is throwing below error
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.
i have checked pg_hba.conf file and one server is using trust method for local access and still not getting error however another server is getting the error with trust and md5 both options.
below are conf files of both servers
Below is my script to create a foreign server
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 $$;
i have checked pg_hba.conf file and one server is using trust method for local access and still not getting error however another server is getting the error with trust and md5 both options.
and
OPTIONS (host 'localhost', port '5432', dbname 'dxpcore', fetch_size '50000');
'localhost' is not the same thing as 'local'. It is instead the same thing as '127.0.0.1' (unless you did something strange with your network config.) The machine not throwing errors is not using 'trust', because it is not connecting through unix sockets. It is connecting through TCP loopback device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.