Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
A foreign table looks like a local table, but each query makes postgres_fdw open a new connection using the OPTIONS of CREATE SERVER (host, port, dbname) and CREATE USER MAPPING (user, password). If that connection fails, the query fails with 08001: could not connect to server "name".
The first line is always the same. The real reason is in DETAIL, which carries the connection error. We reproduced three of them on PostgreSQL 17. The message is not translated, so the Korean and Japanese pages show it in English too.
01Wrong port in the foreign server options
The server erp was created with port '5433', but the database listens on 5432. SELECT * FROM erp_items failed, and DETAIL said port 5433 failed: Connection refused.
Setup
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS erp CASCADE;
CREATE DATABASE erpdb;
CREATE ROLE erp_reader LOGIN PASSWORD 'Erp#2026';
\c erpdb
CREATE TABLE items (item_id int PRIMARY KEY, item_name text);
INSERT INTO items VALUES (1, 'bolt'), (2, 'nut');
GRANT SELECT ON items TO erp_reader;
\c lab
CREATE SERVER erp FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '127.0.0.1', port '5433', dbname 'erpdb');
CREATE USER MAPPING FOR lab_a SERVER erp
OPTIONS (user 'erp_reader', password 'Erp#2026');
GRANT USAGE ON FOREIGN SERVER erp TO lab_a;CREATE EXTENSION
NOTICE: 00000: server "erp" does not exist, skipping
DROP SERVER
CREATE DATABASE
CREATE ROLE
You are now connected to database "erpdb" as user "postgres".
CREATE TABLE
INSERT 0 2
GRANT
You are now connected to database "lab" as user "postgres".
CREATE SERVER
CREATE USER MAPPING
GRANTCREATE FOREIGN TABLE erp_items (item_id int, item_name text)
SERVER erp OPTIONS (schema_name 'public', table_name 'items');CREATE FOREIGN TABLESELECT * FROM erp_items;ERROR: 08001: could not connect to server "erp"
DETAIL: connection to server at "127.0.0.1", port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?SELECT srvname, srvoptions FROM pg_foreign_server WHERE srvname = 'erp'; srvname | srvoptions
---------+-----------------------------------------
erp | {host=127.0.0.1,port=5433,dbname=erpdb}
(1 row)pg_foreign_server.srvoptions shows what postgres_fdw uses: {host=127.0.0.1,port=5433,dbname=erpdb}.
ALTER SERVER erp OPTIONS (SET port '5432');ALTER SERVERALTER SERVER erp OPTIONS (SET port '5432') changed only that option. The same SELECT then returned 2 rows (bolt, nut).
SELECT * FROM erp_items; item_id | item_name
---------+-----------
1 | bolt
2 | nut
(2 rows)02Wrong password in the user mapping
The user mapping for lab_a stored a password that did not match erp_reader. DETAIL showed the remote server's answer: FATAL: password authentication failed for user "erp_reader".
Setup
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS erp CASCADE;
CREATE DATABASE erpdb;
CREATE ROLE erp_reader LOGIN PASSWORD 'Erp#2026';
\c erpdb
CREATE TABLE items (item_id int PRIMARY KEY, item_name text);
INSERT INTO items VALUES (1, 'bolt'), (2, 'nut');
GRANT SELECT ON items TO erp_reader;
\c lab
CREATE SERVER erp FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '127.0.0.1', port '5432', dbname 'erpdb');
CREATE USER MAPPING FOR lab_a SERVER erp
OPTIONS (user 'erp_reader', password 'erp2026');
GRANT USAGE ON FOREIGN SERVER erp TO lab_a;CREATE EXTENSION
NOTICE: 00000: server "erp" does not exist, skipping
DROP SERVER
CREATE DATABASE
CREATE ROLE
You are now connected to database "erpdb" as user "postgres".
CREATE TABLE
INSERT 0 2
GRANT
You are now connected to database "lab" as user "postgres".
CREATE SERVER
CREATE USER MAPPING
GRANTCREATE FOREIGN TABLE erp_items (item_id int, item_name text)
SERVER erp OPTIONS (schema_name 'public', table_name 'items');CREATE FOREIGN TABLESELECT * FROM erp_items;ERROR: 08001: could not connect to server "erp"
DETAIL: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "erp_reader"ALTER USER MAPPING FOR lab_a SERVER erp
OPTIONS (SET password 'Erp#2026');ALTER USER MAPPINGALTER USER MAPPING FOR lab_a SERVER erp OPTIONS (SET password '...') with the right password fixed it, and the foreign table returned 2 rows.
SELECT * FROM erp_items; item_id | item_name
---------+-----------
1 | bolt
2 | nut
(2 rows)03Wrong dbname in the foreign server options
The server was created with dbname 'erp_db', but the database is named erpdb. Login worked, but DETAIL said FATAL: database "erp_db" does not exist.
Setup
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS erp CASCADE;
CREATE DATABASE erpdb;
CREATE ROLE erp_reader LOGIN PASSWORD 'Erp#2026';
\c erpdb
CREATE TABLE items (item_id int PRIMARY KEY, item_name text);
INSERT INTO items VALUES (1, 'bolt'), (2, 'nut');
GRANT SELECT ON items TO erp_reader;
\c lab
CREATE SERVER erp FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '127.0.0.1', port '5432', dbname 'erp_db');
CREATE USER MAPPING FOR lab_a SERVER erp
OPTIONS (user 'erp_reader', password 'Erp#2026');
GRANT USAGE ON FOREIGN SERVER erp TO lab_a;CREATE EXTENSION
NOTICE: 00000: server "erp" does not exist, skipping
DROP SERVER
CREATE DATABASE
CREATE ROLE
You are now connected to database "erpdb" as user "postgres".
CREATE TABLE
INSERT 0 2
GRANT
You are now connected to database "lab" as user "postgres".
CREATE SERVER
CREATE USER MAPPING
GRANTCREATE FOREIGN TABLE erp_items (item_id int, item_name text)
SERVER erp OPTIONS (schema_name 'public', table_name 'items');CREATE FOREIGN TABLESELECT * FROM erp_items;ERROR: 08001: could not connect to server "erp"
DETAIL: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "erp_db" does not existSELECT datname FROM pg_database WHERE datname LIKE 'erp%'; datname
---------
erpdb
(1 row)pg_database has only erpdb for the pattern erp%, so the name in the server options is a typo.
ALTER SERVER erp OPTIONS (SET dbname 'erpdb');ALTER SERVERALTER SERVER erp OPTIONS (SET dbname 'erpdb') fixed the name, and the SELECT returned 2 rows.
SELECT * FROM erp_items; item_id | item_name
---------+-----------
1 | bolt
2 | nut
(2 rows)FAQ
How do I see the real cause of 08001 in postgres_fdw?
Read the DETAIL line. In our tests it showed Connection refused, password authentication failed or database ... does not exist, each pointing to a different option.
Do I need to recreate the foreign server to change the port or dbname?
No. ALTER SERVER name OPTIONS (SET option 'value') changes one option. Foreign tables and user mappings stay as they are.
Where is the password for postgres_fdw stored?
In the user mapping (CREATE USER MAPPING ... OPTIONS (user, password)). Change it with ALTER USER MAPPING ... OPTIONS (SET password '...').
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12