Maps a local database user to a foreign server, supplying the credentials that the foreign data wrapper uses to access an external data source.
Syntax
CREATE USER MAPPING [IF NOT EXISTS] FOR { user_name | USER | CURRENT_USER | PUBLIC }
SERVER server_name
[ OPTIONS ( option 'value' [ , ... ] ) ]Usage notes
A user mapping stores the connection information — typically a username and password — that the foreign data wrapper combines with the foreign server definition to connect to an external data source.
Who can create a user mapping:
The owner of a foreign server can create a user mapping for any user.
If you have the
USAGEprivilege on a server, you can map your own username to that server.
Parameters
| Parameter | Description |
|---|---|
IF NOT EXISTS | Skips the command without raising an error if a mapping for the specified user and server already exists. Issues a notice instead. Note The existing mapping may not match what the command would have created. |
user_name | The local database user to map. CURRENT_USER and USER resolve to the current session user. Specify PUBLIC to create a fallback mapping used when no user-specific mapping exists. |
server_name | The foreign server to map the user to. |
OPTIONS ( option 'value' [, ... ] ) | Connection options for this mapping — typically the remote username and password. Option names must be unique. Valid option names and values depend on the foreign data wrapper for the server. |
Examples
Map a named user to a server:
CREATE USER MAPPING FOR bob
SERVER foo
OPTIONS (user 'bob', password 'secret');Map the current session user to a server:
CREATE USER MAPPING FOR CURRENT_USER
SERVER production_db
OPTIONS (user 'read_only_user', password 'secure_password');What's next
ALTER USER MAPPING — modify an existing user mapping
DROP USER MAPPING — remove a user mapping
CREATE FOREIGN TABLE — define a foreign table that uses the server and user mapping