PostgreSQL on the gateway (Enterprise)
Enterprise only. This page is about the Portward gateway that a company runs in its own cloud. If Portward runs it for you, on the web, this page doesn’t apply: see the page for Portward on the web.
This page covers PostgreSQL on your own servers, Amazon RDS and Aurora, Azure Database for PostgreSQL and Google Cloud SQL. Portward is tested on PostgreSQL 16 and 17.
Before you start
Section titled “Before you start”- The pgcrypto extension in the database Portward will read. The gateway uses it to compute tokens inside PostgreSQL, so sensitive values never leave the database in plain text on the way to the AI.
- An administrator who can create a role and run the SQL below.
- The database’s host name on the gateway’s allowed hosts (Allow the host).
Network access
Section titled “Network access”The gateway connects to the database’s port over TLS. The port is 5432 by default. Open that port to the gateway as
Network access describes. The gateway always checks the certificate and host name
(sslmode=verify-full).
Create a read-only login
Section titled “Create a read-only login”Connect as an administrator to the database Portward will read, and run the SQL below. Replace sales with your
database’s name and <password> with a strong password. You type the same password into setup later
(Connect it in setup). If someone else types it there, share it through your password
manager, never in chat or email.
CREATE EXTENSION IF NOT EXISTS pgcrypto;CREATE ROLE portward_reader LOGIN PASSWORD '<password>';GRANT CONNECT ON DATABASE sales TO portward_reader;GRANT USAGE ON SCHEMA public TO portward_reader;GRANT SELECT ON ALL TABLES IN SCHEMA public TO portward_reader;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO portward_reader;Run it as the owner of the tables. On RDS, that is usually the master user that created them. Another login can
grant only what it owns, or what it was granted with the right to pass on. To see who owns a table, run \dt public.*
in psql. If another role owns them, run the GRANT lines as that role.
The last line lets the login read tables that the administrator running it creates later. Leave it out if you want
to grant new tables one by one.
Portward reads the tables and views in the login’s current schema, which is the first schema on its search path.
For tables in another schema, such as reporting, grant USAGE and SELECT on that schema instead and put it
first. Keep public after it, because pgcrypto’s functions live there:
ALTER ROLE portward_reader SET search_path = reporting, public;Hosted versions
Section titled “Hosted versions”Amazon RDS and Aurora
Section titled “Amazon RDS and Aurora”- Run the SQL above as the master user. The master user can’t be Portward’s login itself, because it holds
rds_superuser. Any PostgreSQL client that can reach the instance on port 5432 works, such aspsqlon an EC2 instance in the same VPC. In the instance’s security group, allow port 5432 from that machine’s security group. Use Amazon’s CA bundle for the instance’s region. Amazon’s guide Connecting to a DB instance running the PostgreSQL database engine explains how to set up such a machine. Remove that machine’s 5432 rule when you are done.Terminal window curl -fsSO https://truststore.pki.rds.amazonaws.com/us-east-2/us-east-2-bundle.pem # your regionpsql "host=<endpoint> dbname=sales user=<master user> sslmode=verify-full sslrootcert=us-east-2-bundle.pem" - In setup, use the instance or cluster endpoint, such as
sales.abc123xyz.us-east-1.rds.amazonaws.com. Add that exact name to the allowed hosts. - You don’t need to paste a CA. The gateway checks the certificate against Amazon’s RDS CA bundle, which ships with the gateway.
- In the instance’s security group, allow port 5432 from the gateway. For that rule to work, the instance must be in
the stack’s VPC or in a VPC peered with it. On AWS, the gateway’s security group is the stack’s
TaskSecurityGroup. On the stack’s Resources tab, its Physical ID is the group’s id (
sg-…). Its description starts with “Portward gateway”.
Azure Database for PostgreSQL (flexible server)
Section titled “Azure Database for PostgreSQL (flexible server)”- Allow pgcrypto first. In the portal, open the server and go to Server parameters. Find
azure.extensions, tickPGCRYPTOand save. Then run the SQL above. - The admin login you created with the server can’t be Portward’s login, because it is a member of
azure_pg_admin. - You don’t need to paste a CA. Azure’s certificate comes from a public CA.
- Under Networking, allow the gateway’s address in the firewall rules. You can also reach the server through a private network the gateway can use.
Google Cloud SQL for PostgreSQL
Section titled “Google Cloud SQL for PostgreSQL”Cloud SQL’s server certificates come from Google’s own CA. You can connect in two ways, and which ones work depends on where the gateway runs.
-
Directly, with the shared CA (every install). This works when the instance uses shared CAs (
--server-ca-mode=GOOGLE_MANAGED_CAS_CA). Their certificates carry the instance’s DNS name. Connect to that DNS name and paste the CA into Server CA certificate (PEM). Get the CA withgcloud sql instances describe <instance> --format="value(serverCaCert.cert)". In the console, you can also go to Connections, then Security, then Download. On the default per-instance CA, an instance’s certificates name no host, so the host name check refuses them. Google lets you switch an existing instance to shared CAs when you edit it. See Manage SSL/TLS certificates. -
Through the Cloud SQL Auth Proxy (your own server or the Azure VM only). Run the Cloud SQL Auth Proxy as a service in the gateway’s
compose.yaml. It shares the gateway’s network (network_mode: "service:gateway") and uses a service account with the Cloud SQL Client role. The proxy encrypts the connection to Cloud SQL itself, and it uses that IAM role instead of an address allow-list. In setup, connect to host127.0.0.1with the read-only login, and add127.0.0.1to the allowed hosts. The proxy has to run on the gateway’s own machine. The gateway skips TLS only for127.0.0.1, and every other host must present a certificate that names it.This doesn’t work on the Google Cloud or AWS installs. On Google Cloud, the startup script rewrites
compose.yamlat every boot and removes services it didn’t write, so the proxy disappears at the next restart. On AWS, the gateway runs on ECS Fargate, which has no Compose file. On your own server and the Azure VM,compose.yamlkeeps your changes, andportward updatechanges only the gateway’s image line.
The postgres user can’t be Portward’s login, because it is a member of cloudsqlsuperuser. Create
portward_reader with the SQL above.
Connect it in setup
Section titled “Connect it in setup”Choose PostgreSQL and fill in these fields. Name is the name people and the AI see, such as sales. Then
fill in Host, Port (empty means 5432), Database, User (portward_reader) and Password. Paste
Server CA certificate (PEM) only for a company CA or Cloud SQL’s CA.
What the setup check tests
Section titled “What the setup check tests”Before setup saves, it connects with the login and runs these checks in order:
- The host is on the allowed hosts. If it isn’t, setup says “isn’t in the hosts the gateway operator allowed for setup”. An RDS or Aurora name that only a wildcard allows gets “is on a domain other customers share”.
- Setup can connect. Each failure has its own message: “login failed: check the user and password”, “the server’s TLS certificate couldn’t be verified”, “that database doesn’t exist on the server”, “the server doesn’t accept this login from the gateway’s address”, “the host name can’t be found”, or “the server didn’t answer; check the host, port and its firewall”.
- The connection is encrypted (“use TLS”).
- The login is not a superuser (“connect with a read-only role, not a superuser”).
- The login is not an administrator (“connect with a read-only role, not an administrator”). This check covers
CREATEROLE,BYPASSRLS, and membership inpg_write_all_data,pg_database_owner,azure_pg_admin,rds_superuser,cloudsqlsuperuseroralloydbsuperuser. - pgcrypto is installed (“run CREATE EXTENSION pgcrypto in this database first”).
- The login can read at least one table or view in its current schema (“the role can’t read any tables in this
database”). Setup lists any table the login could
INSERT,UPDATE,DELETEorTRUNCATEas one it could change, and you can’t expose that table.
Each time the gateway starts, it checks again that the login is not a superuser and can’t change any table it exposes.