How to Set Up and Connect Metasploit with PostgreSQL on Kali Linux for Advanced Penetration Testing

Metasploit is a powerful penetration testing framework, but without a database, it cannot efficiently store and manage scanned hosts, services, and vulnerabilities. Integrating Metasploit with PostgreSQL allows ethical hackers to organize and retrieve scan results effectively. This guide provides a step-by-step approach to installing and configuring PostgreSQL on Kali Linux, creating a Metasploit database, connecting Metasploit to the database, troubleshooting common errors, and setting up automatic database connections. By following this guide, security professionals can optimize their penetration testing workflow and utilize Metasploit's full potential.

Mar 28, 2025 - 12:41
Updated: 1 month ago
104.4k
How to Set Up and Connect Metasploit with PostgreSQL on Kali Linux for Advanced Penetration Testing

Quick answer: To connect Metasploit to PostgreSQL on Kali Linux, install and start PostgreSQL, create a dedicated database and user, then connect from msfconsole with db_connect and confirm with db_status. If you see no connection, recheck the credentials, then set up a persistent configuration so it reconnects each time you start Metasploit.

Key takeaways

  • Start the PostgreSQL service first, then check the link in msfconsole with db_status.
  • Create a dedicated database and user instead of using the postgres superuser.
  • If you see No database connection, the service is usually stopped or the credentials are wrong.

Introduction

Metasploit is one of the most powerful penetration testing frameworks used by ethical hackers and security professionals. However, to improve its functionality, integrating Metasploit with a PostgreSQL database is important.

By default, Metasploit can run without a database, but this limits its ability to store and retrieve scan results, exploit data, vulnerabilities, and service information efficiently.

Why Integrate Metasploit with PostgreSQL?

Setting up a database connection allows you to:

  • Store and organize scan results

  • Retrieve information quickly

  • Run advanced queries for better data analysis

  • Automate penetration testing workflows

This guide gives step-by-step instructions to install, configure, and connect Metasploit to PostgreSQL on Kali Linux.

Step 1: Install and Start PostgreSQL

Install PostgreSQL

Ensure PostgreSQL is installed on your Kali Linux system:

sudo apt update && sudo apt install postgresql postgresql-contrib -y

Start PostgreSQL Service

After installation, start the PostgreSQL service:

sudo systemctl start postgresql

How Do You Verify PostgreSQL is Running?

To confirm that the service is running, check its status:

sudo systemctl status postgresql

How Do You Enable PostgreSQL at Boot?

To make PostgreSQL start automatically on system boot:

sudo systemctl enable postgresql

Step 2: Create a Metasploit Database and User

Metasploit requires a dedicated PostgreSQL database and user. Follow these steps:

Switch to PostgreSQL User

sudo -i -u postgres

Access PostgreSQL Command Line

psql

Create a New Database User

Replace msf_user with your preferred username and yourpassword with a strong password:

CREATE USER msf_user WITH PASSWORD 'yourpassword';

Create a Metasploit Database

CREATE DATABASE msf_database OWNER msf_user;

Grant Privileges to the User

GRANT ALL PRIVILEGES ON DATABASE msf_database TO msf_user;

Exit PostgreSQL Shell

\q
exit

Step 3: Connect Metasploit to PostgreSQL

Now, start Metasploit:

msfconsole

Inside msfconsole, connect to the database:

db_connect msf_user:[email protected]:5432/msf_database

To confirm the connection, run:

db_status

If the setup is successful, you should see:

[*] Connected to msf_database. Connection type: postgresql.

Step 4: Fix "No Database Connection" Issue

If db_status shows "No Database Connection", try the following troubleshooting steps:

1. Ensure PostgreSQL is Running

sudo systemctl status postgresql

If it’s not running, start it:

sudo systemctl start postgresql

2. Check if the Database Exists

List available databases:

sudo -i -u postgres
psql -c "\l"

If msf_database is missing, recreate it using Step 2.

3. Restart Metasploit and Reconnect

exit
msfconsole
db_connect msf_user:[email protected]:5432/msf_database
db_status

Step 5: Configure Persistent Database Connection

Instead of manually connecting every time, configure Metasploit for automatic database connection.

1. Create the Metasploit Configuration Directory

sudo mkdir -p /usr/share/metasploit-framework/config

2. Create and Edit database.yml

sudo nano /usr/share/metasploit-framework/config/database.yml

3. Add the Following Configuration

development:
 adapter: "postgresql"
 database: "msf_database"
 username: "msf_user"
 password: "yourpassword"
 port: 5432
 host: "127.0.0.1"
 pool: 256
 timeout: 5

production:
 adapter: "postgresql"
 database: "msf_database"
 username: "msf_user"
 password: "yourpassword"
 port: 5432
 host: "127.0.0.1"
 pool: 256
 timeout: 5

Save the file (CTRL + X, then Y, and press Enter).

4. Set Correct File Permissions

sudo chmod 644 /usr/share/metasploit-framework/config/database.yml

5. Restart Metasploit and Verify

msfconsole
db_status

If the output shows connected, Metasploit is now set up to automatically use PostgreSQL.

Step 6: Using Metasploit with the Database

Once connected, you can use database-related Metasploit commands:

List Available Workspaces

workspace

Create a New Workspace

workspace -a penetration_test

List All Discovered Hosts

hosts

View Services Running on Hosts

services

Check Discovered Vulnerabilities

vulns

Store Nmap Scan Results in the Database

db_nmap -sV -p- 192.168.1.1

Conclusion

By following this guide, you have successfully:

 Installed and started PostgreSQL on Kali Linux
 Created a database and user for Metasploit
 Connected Metasploit to the database
 Fixed common database connection issues
 Configured Metasploit for automatic database connection
 Learned how to use database features in Metasploit

Now, you can efficiently manage scan results, exploits, vulnerabilities, and services in Metasploit using PostgreSQL. This setup enhances your penetration testing workflow, making it more structured and efficient.

To take this further with guided labs and an instructor, see our Certified Ethical Hacker programme.

Related reading

Reference

For the authoritative details, see Kali Linux documentation.

Frequently Asked Questions

Metasploit can run without a database, but connecting one lets it store hosts, services, vulnerabilities and scan results so you can search and reuse them across sessions. It also supports faster queries and a more organised workflow during an authorised penetration test.

Install PostgreSQL with apt, start it with sudo systemctl start postgresql and enable it at boot with sudo systemctl enable postgresql. Then create a dedicated Metasploit database and user, and connect to them from msfconsole.

Open msfconsole and run db_status. If it shows PostgreSQL connected, you are ready to work. If it reports no connection, check that the service is running and the database credentials are right, then use db_connect or a persistent configuration file.

It stores discovered hosts, open services, vulnerabilities, credentials and scan results, including data imported from tools such as Nmap. You can list them with commands like hosts, services and vulns, which helps organise findings in a lab or authorised test.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.