githubEdit

Pentesting PostgreSQL

Default Port: 5432

PostgreSQL (psql) is an open-source relational database system. It's commonly found in web applications, especially those using Python/Django, Ruby on Rails, and Java/Spring Boot frameworks.


Enumeration

Nmap Scripts

nmap -sV -p 5432 --script="pgsql-*" $ip
nc -nv $ip 5432

Connecting to PostgreSQL

Using psql Client

# Basic connection
psql -h <host> -p 5432 -U <username> -d <database>

# With password in environment variable (avoids password prompt)
PGPASSWORD='password' psql -h 127.0.0.1 -p 5432 -U postgres -d cozyhosting

# Connect to default database
psql -h <host> -U postgres

# Connect via Unix socket (local)
psql -U postgres

Common Default Credentials

Username
Password

postgres

postgres

postgres

(empty)

admin

admin


Essential psql Commands

Command
Description

\l or \list

List all databases

\c <database>

Connect to a database

\dt

List tables in current database

\dt+

List tables with size and description

\d <table>

Describe table structure (columns, types)

\d+ <table>

Describe table with extra info

\du

List users/roles

\dn

List schemas

\df

List functions

\x

Toggle expanded display (vertical output)

\q

Quit psql

Example Workflow


Extracting Data

Dumping Users and Passwords

Searching for Sensitive Data


File Operations (Requires Superuser)

Reading Files

Writing Files


Command Execution

Using COPY FROM PROGRAM (PostgreSQL 9.3+)

Using Extensions


Cracking PostgreSQL Hashes

PostgreSQL password hashes are typically bcrypt ($2a$, $2b$, $2y$).


Privilege Escalation

Check Current User Privileges

PostgreSQL to System Shell

If PostgreSQL is running as root or has SUID, check for privilege escalation:


Useful Resources

  • https://book.hacktricks.wiki/en/network-services-pentesting/pentesting-postgresql.html

  • https://www.postgresql.org/docs/current/app-psql.html

Last updated