To connect to your OCI PostgreSQL Database System (ORA11PG) using psql, you need a few details first.
Step 1: Get PostgreSQL Connection Details
In OCI Console:
Databases → PostgreSQL → ORA11PG
Check:
-
Private IP Address (Example:
192.168.10.50) -
Port (Default:
5432) -
Database Name (Example:
postgres) -
Username (Example:
postgresor admin user created during provisioning)
Step 2: Verify Network Connectivity
From your Compute VM:
ping <postgres_private_ip>
Example:
ping 192.168.10.50
Check port connectivity:
nc -zv 192.168.10.50 5432
or
telnet 192.168.10.50 5432
Expected:
Connection to 192.168.10.50 5432 port [tcp/postgresql] succeeded!
If it fails:
✅ Check NSG/Security List
Allow:
| Source | Protocol | Port |
|---|---|---|
| Compute Subnet CIDR | TCP | 5432 |
Step 3: Install PostgreSQL Client (psql)
Oracle Linux / RHEL
sudo dnf install postgresql -y
Verify:
psql --version
Example:
psql (PostgreSQL) 17.6
Step 4: Connect Using psql
Syntax:
psql -h <host_ip> -p 5432 -U <username> -d <database>
Example:
psql -h 192.168.10.50 -p 5432 -U postgres -d postgres
Enter password.
Step 5: Verify Connection
After login:
SELECT version();
SELECT current_database();
SELECT current_user;
List databases:
\l
List schemas:
\dn
List tables:
\dt
Exit:
\q
OCI Practical Example
Suppose:
| Component | Value |
|---|---|
| PostgreSQL IP | 192.168.20.80 |
| Port | 5432 |
| User | postgres |
| Database | postgres |
Connection command:
psql -h 192.168.20.80 -p 5432 -U postgres -d postgres
Interview Question
Q: What is required to connect from a Compute Instance to OCI PostgreSQL Database System?
Answer:
- Network connectivity between Compute and PostgreSQL DB System.
- Security List or NSG allowing TCP 5432.
-
PostgreSQL client (
psql) installed. - Valid database username and password.
- Correct private IP/FQDN of the PostgreSQL server.
- Open your OCI Console, navigate to Databases > PostgreSQL > DB Systems. []
- Click on your active instance name: ORA11PG. []
- Look for the Connection details or Endpoint section and copy:
- Log into your OCI Compute VM using SSH. [1]
- Install the
psqlclient if it isn't already installed:- For Ubuntu/Debian:
sudo apt install postgresql-client - For RHEL/Rocky Linux:
sudo dnf install postgresql[]
- For Ubuntu/Debian:
- Run the standard connection string:(Replacebash
psql -h <Your_Private_IP_or_FQDN> -p 5432 -U <Your_Username> -d <Your_Database_Name>Use code with caution.<Your_Private_IP_or_FQDN>with the endpoint gathered in Step 1). [1]
- Navigate to Identity & Security > Bastions in OCI and create a Bastion if you don't have one. [1]
- Create a Port Forwarding Session:
- Copy the SSH command provided by OCI Bastion and run it on your local machine terminal:[, 2]bash
ssh -i <path_to_private_key> -N -L 5432:<database_private_ip>:5432 <bastion_user>@<bastion_endpoint>Use code with caution. - Keep that terminal window open, open a new terminal on your local machine, and run:[, 2]bash
psql -h 127.0.0.1 -p 5432 -U <Your_Username> -d <Your_Database_Name>Use code with caution.
psql connection times out, verify your cloud firewall rules: [1]- Ingress Security List: Go to the Security List of your PostgreSQL database's private subnet. Make sure there is an Ingress Rule allowing TCP traffic on port 5432 from your Compute VM's IP or the Bastion's subnet
No comments:
Post a Comment