How to Connect to AWS RDS From Your Local Machine
A laptop is not in the VPC. An RDS instance in a private subnet is not on the internet. To connect to RDS from local machine you bend one of those two facts, and you should decide which one, and for how long.
This is that decision for PostgreSQL and MySQL: a public endpoint you lock down, an SSH bastion, Session Manager with no inbound SSH, and IAM database tokens. The shell commands are the forms AWS documents. They were not run against an AWS account from the machine that checked this article. The failure list at the end is the set of causes that actually show up once the command is right and the connection is still refused. AWS’s own overview is Connecting to an Amazon RDS DB instance.
Pick your method
| Situation | Use this | Do not use this |
|---|---|---|
| A short-lived demo, one address, you accept a public endpoint | Public access, security group limited to your current IP, SSL on | A security group open to the world |
| Production, and you already have a bastion host | SSH local forward through the bastion | Opening the database security group to your home IP |
| Production, no bastion, an instance in the VPC runs the SSM agent | Session Manager port forwarding to the remote host | A new bastion just for this afternoon |
| You want a password that dies in 15 minutes | IAM database authentication, plus one of the network paths above | IAM auth as a substitute for a network path |
| You are going through RDS Proxy | The proxy endpoint, same network path, token aimed at the proxy if IAM is on | The instance endpoint, hoping the proxy policies apply |
“Private” means the instance’s address is only routed inside the VPC. Publicly accessible = no is the setting. A public subnet and a public IP are not enough if that flag is off, and a public flag is not enough if the security group drops the packet.
Do the network first. IAM tokens and SSL settings do not matter until a TCP connection can be established.
Option 1: a public endpoint, locked down
RDS can have a public address. You turn on public accessibility, put the instance in a public subnet with a route to an internet gateway, and add your current IP to the instance security group on the database port. Clients then use the endpoint name from the console.
For PostgreSQL, require SSL:
psql "host=mydb.abc123.us-east-1.rds.amazonaws.com port=5432 user=app dbname=app sslmode=require"
For MySQL 8:
mysql --host=mydb.abc123.us-east-1.rds.amazonaws.com --port=3306 --user=app --ssl-mode=REQUIRED
sslmode=require encrypts the session and does not verify the server name. verify-full, with the RDS CA bundle, also checks that you reached the host you asked for. The bundle and the parameter are in Using SSL/TLS to encrypt a connection to a DB instance. Use verify-full when the endpoint is public. On a tunnel to a name you already typed, require is the usual minimum.
This option is a poor production default. Your IP changes. A /32 rule you added on Monday is wrong on Tuesday, and the temptation is to widen it. The instance is one security-group edit away from the internet. Use it for a sandbox you can delete. For anything with real data, use a private instance and a path that starts inside the VPC.
If company policy allows a public endpoint for production, keep a named security group, review the rules, and prefer a VPN into the VPC over a list of home addresses. That VPN is another network path. It is not covered command-by-command here, because the client side depends on the VPN product.
Option 2: SSH tunnel through a bastion
A bastion is a small instance in a public subnet. Its security group allows SSH from your IP, or from your corporate range. The database security group allows the database port from the bastion’s security group, and from nowhere else. You SSH to the bastion and ask it to open a connection to RDS for you.
Postgres
ssh -N -L 5432:mydb.abc123.us-east-1.rds.amazonaws.com:5432 ec2-user@bastion.example.test
Leave that session running. In another terminal:
psql "host=127.0.0.1 port=5432 user=app dbname=app sslmode=require"
-N means “do not open a shell”. -L means “listen locally and forward to this host and port, from the bastion”. The host in the middle is the RDS endpoint as the bastion sees it, which is the normal DNS name. You do not need the private IP, and you should not paste a private IP into a ticket, because it changes when the instance fails over.
The Postgres client is talking to a forward on your own machine. The TLS certificate is for the RDS hostname, not for that forward, so sslmode=verify-full often fails through a tunnel unless you override the name the checker expects. sslmode=require still encrypts. If you need name verification through a forward, set the libpq hostaddr separately from host, or use a ProxyCommand setup you have actually tested. For most application debugging, require plus a bastion you control is the working combination.
MySQL
ssh -N -L 3306:mydb.abc123.us-east-1.rds.amazonaws.com:3306 ec2-user@bastion.example.test
mysql --host=127.0.0.1 --port=3306 --user=app --ssl-mode=REQUIRED --database=app
MySQL’s default port and Postgres’s default port can both be forwarded. If the local port is already taken, because a database is running on your machine, pick another local port on the left-hand side and pass that port to the client. The right-hand side stays the port RDS is listening on.
Keeping the tunnel alive
Idle forwards get killed by NAT devices. The SSH options that help:
ssh -N \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-o ExitOnForwardFailure=yes \
-L 5432:mydb.abc123.us-east-1.rds.amazonaws.com:5432 \
ec2-user@bastion.example.test
ExitOnForwardFailure makes SSH quit immediately if it cannot bind the local port, instead of sitting there looking connected. That one option explains a lot of “the tunnel is up but nothing listens”.
Use a key, not a password, and restrict the bastion security group. The bastion is a path to the database. Treat it like one: patch it, do not store database passwords on it, and do not use it as a general workstation. If more than a couple of people need it, you will have a better time with Session Manager and IAM, because you can see who started a session.
Option 3: Session Manager, no bastion
AWS Systems Manager Session Manager can port-forward to a remote host through an instance that has the SSM agent, an instance profile, and outbound access to the SSM endpoints. Nothing on that instance needs to accept inbound SSH. The document you want is AWS-StartPortForwardingSessionToRemoteHost.
aws ssm start-session \
--target i-0123456789abcdef0 \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters '{"host":["mydb.abc123.us-east-1.rds.amazonaws.com"],"portNumber":["5432"],"localPortNumber":["5432"]}'
Then use the same psql or mysql command as in the bastion section, aimed at the local port. The instance id is an instance in the VPC that can reach RDS, not the RDS instance itself. RDS does not run the SSM agent. People point --target at a database and wait. It will not register.
The instance profile needs ssmmessages, ssm, and ec2messages, or the managed policy AWS documents for Session Manager. Private subnets need a path to those APIs, which is either a NAT gateway or VPC endpoints. A timeout during start-session is often that path, not the database. The session manager guide is the source for the document name and the permissions: Start a session.
MySQL is the same command with port 3306 on both the remote port and, if it is free, the local port.
Session Manager sessions show up in CloudTrail. An SSH key on a bastion does not, unless you have added that logging yourself. That is the operational reason to prefer SSM when you can.
Option 4: IAM database authentication
IAM authentication replaces the database password with a token that AWS documents as valid for 15 minutes. It does not open a network path. You still need option 1, 2, or 3 before the token is useful.
Turn IAM auth on for the instance or the proxy. Create a database user and grant it the AWS authentication method. For PostgreSQL on RDS, AWS documents:
GRANT rds_iam TO app;
For MySQL, AWS documents identifying the user with AWSAuthenticationPlugin. Copy the statement from the current IAM DB auth page for your engine version rather than from memory. Neither grant exists on a PostgreSQL or MySQL you installed yourself, so neither was run for this article.
Generate a token:
aws rds generate-db-auth-token \
--hostname mydb.abc123.us-east-1.rds.amazonaws.com \
--port 5432 \
--region us-east-1 \
--username app
The output is the password. It is long, it contains characters a shell will mangle, and it expires. Pass it to the client as a password, with SSL required. For psql, PGPASSWORD from a command substitution works if you quote it. For MySQL, --password=... on the command line puts the token in the process list. Prefer a prompt, or a defaults file with permissions restricted to you, and delete it when the token is dead.
The database user name in the token must match the user you connect as. The hostname in the token must match the endpoint you are authenticating to. A token for the instance will not authenticate to the proxy, and the reverse is also true. Region and port are part of the signature. A token generated in the wrong region fails in a way that looks like a bad password.
Tokens and ordinary passwords can coexist on different users. Keep a password user for break-glass, stored somewhere that is not the laptop, and use the IAM user for daily work. Rotating is then “stop generating tokens for that IAM role”, which you can do without a database outage.
Automate it
The moving parts are the tunnel and the token. Both expire or drop. Typing them each morning is how people end up with a security group they opened “just for today”.
A small script you run before connecting can do both jobs. One pattern:
- Start the SSH forward or the SSM session if the local port is not already accepting connections.
- If the user is an IAM user, generate a fresh token and write it somewhere the client will read, with file permissions limited to you.
- Start
psqlormysqlagainst the forward, with SSL.
Keep the database password out of the script. The script’s job is to fetch a token or to open a path, not to become a second copy of the secret. If you use a password user, let the client prompt, or use the operating system’s keychain.
MoleDB runs an Access script before a connection opens. The script can start the tunnel, or fetch a fresh credential such as an IAM token, and then you connect. Your data stays on your Mac.
Once the session works, the usual next task is a realistic dataset rather than a query against production. Copying a bounded set of rows is covered in pg_dump where clause.
A script does not replace a read-only database user. Automation makes it easier to reach the database. It should not make it easier to drop a table. Create a user that can SELECT, and use a writer only when you mean to write. That user is ordinary PostgreSQL and MySQL privilege work. It belongs in its own notes.
Which client you type into, once the path exists, is the other decision. Best Postgres client for Mac compares the GUIs on that point, including what they do with a connection you have marked as production.
When it will not connect
Go in order. Later checks waste time if an earlier one is false.
Security group. The instance security group needs an inbound rule for the database port. The source is your IP (public endpoint), the bastion security group (SSH), or the SSM instance’s security group (Session Manager). A rule that names the wrong security group looks identical to a timeout. A rule on the network ACL is not a substitute. Check the instance’s group, not only the VPC default group.
Subnet and route. Publicly accessible must be yes if you are not tunneling, and the subnet needs a route to an internet gateway. For a private instance, you should be unable to reach it directly. A timeout from your laptop and a success from the bastion is the healthy state. The reverse means the bastion cannot route to the database subnet. Check the route table on the bastion’s subnet and the database subnet.
Network ACL. NACLs are stateless. An inbound allow on the database port with no outbound allow for the ephemeral ports produces a timeout that looks like a security group. Default NACLs allow all. Custom ones are where this hides.
DNS. Use the endpoint hostname. It resolves to a private address from inside the VPC and, if the instance is public, to a public address from outside. Hard-coding an IP breaks on failover. psql saying the server closed the connection immediately is sometimes a proxy or a tunnel forwarding to the wrong port, not a DNS failure. DNS failures say they cannot resolve the name.
TLS. RDS parameter rds.force_ssl (PostgreSQL) and require_secure_transport (MySQL) make a plain connection fail after TCP succeeds. The error mentions SSL or a secure transport, which is how you tell it from a timeout. Turn SSL on in the client. Do not turn the server requirement off to get in.
Timeouts versus refusal. A timeout is a packet that never came back: security group, NACL, route, or wrong host. A connection refused is a host that answered and had nothing listening, which is rare for RDS and common when your SSH forward is aimed at a port the bastion itself is not serving. An authentication error means the network and TLS are done. Stop changing security groups and read the password, token, and username.
IAM. Token older than 15 minutes, wrong hostname, wrong region, wrong port, user without the IAM grant, or SSL off. Generate a new token and connect within a minute while you are debugging. Clock skew on the laptop breaks the signature.
RDS Proxy. Connect to the proxy endpoint. The proxy security group must allow you, and must be allowed into the database. Pinning a session (a prepared statement the proxy cannot multiplex, or a SET it does not expect) is a behaviour problem after you are connected, not a timeout. If the proxy requires IAM, the token hostname is the proxy.
The client library. psql and mysql tell you which of the above you hit. A GUI that says “could not connect” and nothing else is hiding the useful line. Ask it for the details, or reproduce with the CLI once. When the CLI works and the GUI does not, the GUI is using a different host, port, SSL mode, or a stale saved password.
After you are in, run SELECT current_user, current_database(); (PostgreSQL) or SELECT CURRENT_USER(), DATABASE(); (MySQL) and confirm you are the user you think you are, on the database you think you opened. A surprising number of “the table is empty” reports are a connection to the wrong database on the right instance.
FAQ
How do I connect to a private RDS instance from my local machine?
A private instance has no route from the internet. Give your machine a path that starts inside the VPC. The usual paths are an SSH local forward through a bastion, or a Session Manager session that forwards to the remote host. Publicly accessible plus an open security group will also work, and it is the wrong default for production. IAM authentication changes the password, not the network path.
Why can't I connect to RDS from local?
The security group, the route, and TLS are the three that account for most failures. The instance security group must allow your source on the database port. A private subnet will not answer a laptop on the internet. If the server requires SSL, a client that does not negotiate it is refused even when the TCP connection succeeds. Check the endpoint name, the database name, and whether the token or password has expired.
How do I connect to RDS Proxy from local?
You connect to the proxy endpoint, not the instance endpoint. The proxy still has to be reachable, so the same bastion or Session Manager path applies. Security groups on the proxy must allow your source, and the proxy's security group must be allowed into the database. If the proxy has IAM authentication on, generate the token for the proxy hostname. The database user needs rds_iam only if the proxy uses end-to-end IAM authentication. A proxy that logs in with a Secrets Manager password must not grant rds_iam to that user.
How do I create an SSH tunnel for PostgreSQL?
From a machine that can SSH to a host inside the VPC, open a local forward from a port on your computer to the RDS endpoint's Postgres port, then point psql at that local forward with sslmode set. The bastion section has the command. Keep the SSH session open while you work. A ServerAliveInterval stops a quiet session from being dropped. The database security group must allow the bastion, not your laptop's public address.

