CREATE SERVER
A foreign server names a remote database — ClickHouse or PostgreSQL — or a database on disk — a DuckDB file or the directory of a SereneDB database — together with what is needed to reach it, and stores that definition in the catalog. Once created, the server is a catalog you query like any other: server_name.schema.table.
The difference from ATTACH is persistence and access control. An attachment is session-level state that disappears when the server process restarts, and it carries no privileges of its own. A foreign server is catalog DDL: it survives restart, re-connects automatically on boot, has an owner, and is protected by USAGE privileges.
ATTACH | CREATE SERVER | |
|---|---|---|
| Survives restart | No — re-attach every session | Yes — replayed on boot |
| Credentials | In the connection string | In the server's OPTIONS |
| Owner | None | The creating role |
| Access control | None | USAGE on the server |
| Catalog visibility | SHOW DATABASES | pg_foreign_server + SHOW DATABASES |
CREATE SERVER
Syntax
Create a server over ClickHouse:
CREATE SERVER analytics FOREIGN DATA WRAPPER clickhouse_fdw
OPTIONS (host 'clickhouse.internal', port '9000', database 'events');
…and over PostgreSQL:
CREATE SERVER orders FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'pg.internal', port '5432', database 'shop', user 'reader', password 'secret');
Foreign-data wrappers
The wrapper name selects the connector. Five are implemented:
FOREIGN DATA WRAPPER | Connects to |
|---|---|
clickhouse_fdw | ClickHouse, over its native protocol |
duckdb_fdw | A DuckDB database file; see DuckDB files |
iceberg_fdw | An Iceberg REST catalog; its options and authentication are on Iceberg Catalog Authentication |
postgres_fdw | PostgreSQL, over the wire protocol |
serenedb_fdw | A database in the data directory of another SereneDB server, read-only; see Read-only SereneDB databases |
Any other wrapper name is rejected at CREATE SERVER time.
Server names
A server name is a bare identifier — servers are not schema-qualified, so CREATE SERVER a.b is a syntax error rather than a server named a.b. Reserved keywords must be quoted (CREATE SERVER "select" …).
The name is also the attach alias, and that alias is instance-wide: it shares a namespace with SereneDB databases and with the aliases created by ATTACH. Two servers cannot share a name, and a server cannot take the name of an existing database.
OPTIONS
OPTIONS is a list of key 'value' pairs. Keys are identifiers — unquoted or quoted, and normalized to lower case, so "PASSWORD" and password are the same key. Values are always single-quoted strings, including numeric ones like port '9000'.
Common keys, with the aliases each connector accepts:
| Option | Aliases | Meaning |
|---|---|---|
host | hostname | Remote host |
port | Remote port | |
user | username | Remote role |
password | passwd | Password for that role |
database | dbname, db | Remote database |
secure | ssl (ClickHouse) | Use TLS |
Keys beyond this list are passed through to the connector, so any connection parameter the underlying connector understands (for example sslpassword, connect_timeout) can be set the same way. An option the connector does not recognize surfaces as a connection error at CREATE SERVER. A clickhouse_fdw server also takes compression (none, zstd or lz4), and takes every option it leaves out from the unnamed clickhouse secret, when there is one.
Values are stored verbatim and are not parsed further, so a value containing spaces (password 'pass word') round-trips intact.
Connectivity is validated eagerly
CREATE SERVER connects to the remote before persisting anything. If the connection fails — wrong host, unknown role, bad password — the statement raises the connector's error and leaves behind neither a catalog row nor an attachment:
-- Rejected: no such role on the remote. Nothing is persisted.
CREATE SERVER orders FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'pg.internal', port '5432', database 'shop', user 'nosuchrole');
This makes a successful CREATE SERVER a positive statement about reachability, not just a recorded intention.
IF NOT EXISTS
IF NOT EXISTS makes the statement a no-op when the name is already taken. The check happens before the connection attempt, so the OPTIONS of a skipped CREATE SERVER are never applied — an existing server is not reconfigured by re-running CREATE SERVER IF NOT EXISTS with different options.
Querying through a server
A server behaves as a catalog. Reference remote objects with the server name in the catalog position:
SELECT id, val FROM analytics.events.pageviews ORDER BY id;
Where the connector supports writes, DDL and DML work through the server too — the PostgreSQL connector, for example, accepts schema and table creation:
CREATE SCHEMA orders.staging;
CREATE TABLE orders.staging.import (id INTEGER PRIMARY KEY, val TEXT);
INSERT INTO orders.staging.import VALUES (1, 'first');
Filter pushdown applies as it does for an attachment: equality, IN and other supported predicates are translated into the remote query rather than filtered locally.
To see the queries sent to the remote, enable the PostgresQueryLog or ClickHouseQueryLog log type with the memory log storage, and read them, with their durations, from sdb_logs_parsed. The server logs to stdout by default, which sdb_logs_parsed cannot read. Both choices apply to the whole server: until CALL enable_logging(storage = 'stdout') switches back, its log holds only this log type, in memory.
CALL enable_logging('ClickHouseQueryLog', storage = 'memory');
SELECT id, val FROM analytics.events.pageviews ORDER BY id;
SELECT query, duration_ms FROM sdb_logs_parsed('ClickHouseQueryLog');
DuckDB files
duckdb_fdw attaches a DuckDB database file — the file ATTACH opens — as a server, so it is re-attached on boot and guarded by USAGE. path names the file and is required; read_only 'true' opens it for reading only:
CREATE SERVER sales FOREIGN DATA WRAPPER duckdb_fdw
OPTIONS (path '/data/sales.duckdb', read_only 'true');
SELECT count(*) FROM sales.main.orders;
Without read_only a local file is opened for reading and writing, and a remote one (https://, s3://, …) for reading only.
Read-only SereneDB databases
serenedb_fdw attaches a database from the data directory of another SereneDB server — the directory of a stopped server, a copy of it, or a disk snapshot — and serves it read-only. Its tables, views and inverted indexes are queried as usual:
CREATE SERVER archive FOREIGN DATA WRAPPER serenedb_fdw
OPTIONS (path '/mnt/snapshots/shop', database 'shop');
SELECT id FROM archive.public.docs WHERE body @@ 'invoice';
path is required and names the data directory, the directory the other server was started with. database names the database to attach and defaults to postgres. ATTACH '<path>' AS archive (TYPE serenedb, READ_ONLY, DATABASE 'shop') attaches the same database until DETACH or a restart of the server.
A copy of the data directory is complete when it holds engine_v1/wal and engine_v1/store, which holds the index files of the database's tables and of the inverted indexes on its views. A database whose files are not all there is refused, and the error names the missing file. To copy the directory of a running server, run pg_backup_start first, copy engine_v1/wal before engine_v1/store, and run pg_backup_stop once the copy is done.
Tables and inverted indexes are read from the data directory only. A database is refused when one of its tables or inverted indexes is placed in a tablespace other than the database's own. A database created in a tablespace keeps the index files of its tables and indexes in <system identifier>/engine_v1/store at the tablespace's location: copy them into the engine_v1/store of the copy to attach it.
What a read-only database does
- It shows every committed transaction. The database reads the other server's log, so it holds every transaction that server committed before it stopped or before the copy was taken, also rows still kept in memory there. A copy taken while that server wrote shows the transactions committed before the copy reached the end of the log.
- Nothing on disk changes. The data directory is only read: what the database writes while it serves queries stays in memory, and no compaction or other maintenance runs on it.
- Writes are refused.
INSERT,UPDATE,DELETE,TRUNCATE,MERGE, other DDL andnextvalfail as on any read-only attachment, andCREATE INDEX,VACUUM,REINDEXandes_refreshfail withit is attached read-only. Jobs and subscriptions of the database do not run. - Names resolve in this server. A view of the database reads the relations it names without a database from the database itself; a relation it names with a database, its own name included, is read from the database of that name in this server.
USAGEon the server is the only privilege checked on the database's own objects. Owners and grants recorded in the directory belong to the other server's roles and are not used, so a view of the database reads with the privileges of the role that runs the query: a table outside the database that the view reads needs that role'sSELECT.has_table_privilegeand the other privilege functions report every privilege on the database's own objects. The system catalogs in itspg_catalogandinformation_schemakeep the privileges they have in any database.DROP SERVERonly detaches. It never removes files from the directory.
Many readers or one writer
The data directory's PG_VERSION file is locked shared by every read-only attachment and exclusively by the server that owns the directory, so the two exclude each other:
- Any number of read-only attachments, in one server or in several, can read the same data directory at once.
CREATE SERVERfails while a server runs on the data directory.- A server does not start while its data directory is attached read-only, in this server or another.
- A server never attaches its own data directory read-only, also through a symbolic link, a hard-linked copy or another mount of the same files.
The lock is the file system's: on a file system without file locks nothing detects a running server, so attach a copy, a disk snapshot or the directory of a stopped server there.
CREATE SERVER refuses a data directory:
- of a running server, the attaching server included,
- without
PG_VERSIONorengine_v1/wal, - without a database of the given name,
- of a database with a table or an inverted index placed in a tablespace, or
- missing the directory of one of the database's tables or inverted indexes.
Persistence and boot replay
A foreign server is stored in the catalog, so it survives a restart — including an unclean one. On boot, SereneDB replays its foreign servers and re-connects each one, so queries through the server work again without any re-ATTACH:
CREATE SERVER analytics FOREIGN DATA WRAPPER clickhouse_fdw
OPTIONS (host 'clickhouse.internal', port '9000', database 'events');
-- restart serened, then:
SELECT count(*) FROM analytics.events.pageviews; -- still works
If a remote is unreachable at boot, the replay for that server is skipped rather than failing startup; the catalog row remains and queries through the server error until the remote returns.
DROP SERVER
Syntax
DROP SERVER removes the catalog row and, when its transaction commits, detaches the live connection, so the name is reusable from then on. In the transaction that dropped it, a CREATE SERVER of the same name fails with database with name "..." already exists:
DROP SERVER analytics;
After the drop the server is gone from pg_foreign_server and from SHOW DATABASES, and querying through it errors. DROP SERVER IF EXISTS on a missing server is a no-op.
RESTRICT is the default. CASCADE is accepted for PostgreSQL compatibility; because nothing in SereneDB can depend on a foreign server — there are no user mappings and no foreign tables — the two behave identically today.
What DROP SCHEMA and DROP DATABASE do
Foreign servers are children of the database, not of a schema (matching PostgreSQL, whose pg_foreign_server has no namespace column). That determines which cascades reach them:
| Statement | Effect on foreign servers |
|---|---|
DROP SCHEMA … CASCADE | None — servers are untouched and keep serving |
DROP DATABASE … | Removes the database's servers and detaches their connections |
DROP SERVER … | Removes that server and detaches it |
-- The server outlives the schema, cascade or not.
DROP SCHEMA public CASCADE;
SELECT count(*) FROM analytics.events.pageviews; -- still served
Catalog visibility
Created servers appear in pg_foreign_server, scoped to the current database:
SELECT srvname FROM pg_foreign_server WHERE srvname = 'analytics';
srvowner holds the owning role, and srvoptions is a text[] of key=value entries in the order they were given:
SELECT r.rolname, s.srvoptions
FROM pg_foreign_server s JOIN pg_roles r ON s.srvowner = r.oid
WHERE s.srvname = 'analytics';
pg_foreign_data_wrapper lists the three wrappers, and srvfdw points at the server's one, so psql's \des and \dew and information_schema.foreign_servers show them. Every role may use the wrappers: each grants USAGE to PUBLIC, and CREATE SERVER needs the CREATE privilege on the database instead. See System Table Compatibility.
Privileges
A foreign server is owned by the role that created it and carries a single privilege, USAGE.
| Action | Requirement |
|---|---|
CREATE SERVER | CREATE on the current database |
DROP SERVER | Ownership of the server, or superuser |
| Querying through the server | USAGE on the server (owner and superusers exempt) |
GRANT / REVOKE USAGE | Ownership, WITH GRANT OPTION, or superuser |
Because servers are database children, the create gate is a database grant — granting CREATE on a schema does not allow it:
GRANT CREATE ON DATABASE shop TO analyst;
USAGE is checked at query time, so granting and revoking it takes effect on the next statement:
GRANT USAGE ON FOREIGN SERVER analytics TO analyst;
REVOKE USAGE ON FOREIGN SERVER analytics FROM analyst;
Without it, a query through the server fails with permission denied for foreign server analytics. USAGE is the only privilege the object accepts — GRANT SELECT ON FOREIGN SERVER … is rejected as an invalid privilege type.
Differences from PostgreSQL
- No
USER MAPPING. Credentials live in the server'sOPTIONSand one shared connection serves every authorized role, the way a ClickHouse connection is normally shared.CREATE USER MAPPINGandDROP USER MAPPINGare syntax errors; per-role remote identities are not available. - No
CREATE FOREIGN TABLE. Remote tables are reached through the server's catalog (server.schema.table), discovered live, rather than declared one by one. - No
ALTER SERVER, noCREATE OR REPLACE SERVER. Drop and recreate to change options, in two transactions: aCREATE SERVERof the name aDROP SERVERin the same transaction removed fails until that transaction has committed. - No
CREATE FOREIGN DATA WRAPPER. The wrappers are built in. pg_foreign_serveris superuser-only, and its option values are unredacted (see above).CREATE SERVERconnects eagerly, so it fails on an unreachable remote instead of deferring the error to first use.
See also
ATTACHandDETACH— the session-level, non-persistent alternativeGRANT·REVOKE· Privileges- PostgreSQL integration
- Indexing External Data — building a search index over a foreign server's tables