How to Fix "Connection Already Established" Error in Metasploit | Troubleshooting Database Connection Issues in Kali Linux

Metasploit, the powerful penetration testing framework, requires a PostgreSQL database for storing scan results, vulnerabilities, and exploits. However, it allows only one database connection at a time. If you try to establish a new connection without disconnecting the existing one, you may encounter the "Connection Already Established" error. This guide provides step-by-step instructions to troubleshoot and resolve this issue, including how to disconnect the current connection, reconnect with a different database or user, fix database corruption issues, and restart Metasploit services. By following these solutions, you can ensure a smooth and persistent database connection in Metasploit on Kali Linux.

How to Fix "Connection Already Established" Error in Metasploit |  Troubleshooting Database Connection Issues in Kali Linux

Introduction

Metasploit, a powerful penetration testing framework, requires a PostgreSQL database to store scanned hosts, vulnerabilities, and exploit results. However, Metasploit only allows one active database connection at a time. If you try to establish a new connection while another is active, you may encounter the following error:

[-] Error: Connection already established

This issue often arises when switching databases, using a different user, or recovering from a crash. In this guide, we will cover why this error occurs and how to troubleshoot it using db_disconnect, reconnecting with a different database, fixing corruption issues, and restarting Metasploit services.

Why Does Metasploit Allow Only One Database Connection at a Time?

Metasploit’s database management system is designed to support only a single active connection to PostgreSQL at a time. This ensures:

  • Data integrity and prevents conflicts when querying information.

  • Proper indexing and storage of hosts, services, and exploits.

  • Avoidance of duplicate database instances that could slow down performance.

If you try to connect to a different database without disconnecting the current session, Metasploit returns the "Connection already established" error.

How to Disconnect the Current Database Connection in Metasploit

To resolve this issue, you must first disconnect the existing connection before connecting to a new database.

Step 1: Check the Current Database Connection

Inside msfconsole, run:

db_status

If a connection is active, you will see:

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

Step 2: Disconnect the Database Connection

Run the following command:

db_disconnect

You should see:

[*] Successfully disconnected from the database.

Step 3: Verify the Disconnection

Run db_status again:

db_status

If the database is successfully disconnected, you will see:

[*] No Active Database Connection.

How to Reconnect to a Different Database or User

Once you have disconnected from the current database, you can connect to a new one with a different user or database.

Step 1: Connect to a Different Database

db_connect new_user:[email protected]:5432/new_database

For example, if you created a new database called pentest_db with user pentester, use:

db_connect pentester:[email protected]:5432/pentest_db

Step 2: Verify the New Connection

Check if the new database is connected successfully:

db_status

If successful, the output will confirm the new database connection.

Fixing Database Corruption Issues If Metasploit Crashes

If Metasploit crashes while connected to the database, it may leave a corrupted session that prevents a new connection. Follow these steps to fix the issue.

Step 1: Restart PostgreSQL

sudo systemctl restart postgresql

Step 2: Check the Database Integrity

Log into the PostgreSQL shell:

sudo -i -u postgres
psql

List available databases:

\l

If you see any errors or missing databases, drop and recreate the database:

DROP DATABASE msf_database;
CREATE DATABASE msf_database OWNER msf_user;

Step 3: Exit and Reconnect

Exit PostgreSQL with:

\q

Then reconnect in Metasploit:

db_connect msf_user:[email protected]:5432/msf_database

Restarting Metasploit Services to Reset the Connection

Sometimes, restarting Metasploit services can clear database connection issues.

Restart PostgreSQL

sudo systemctl restart postgresql

Restart Metasploit Framework

If restarting PostgreSQL doesn’t work, restart Metasploit:

exit
msfconsole

Now check the connection again:

db_status

Conclusion

The "Connection Already Established" error in Metasploit occurs because only one database connection can be active at a time. To resolve this, you need to:

  • Disconnect the existing connection using db_disconnect.

  • Reconnect to a new database with db_connect.

  • Fix corruption issues if Metasploit crashes by restarting PostgreSQL.

  • Restart Metasploit services if needed to reset the connection.

By following these steps, you can effectively manage database connections in Metasploit, ensuring smooth penetration testing workflows.

Frequently Asked Questions (FAQs)

Why does Metasploit show the "Connection Already Established" error?

Metasploit allows only one active database connection at a time. If you try to establish a new connection without disconnecting the previous one, it triggers this error.

How can I check if a database is already connected in Metasploit?

Run the command db_status in Metasploit. If a connection is active, it will display the connected database details.

How do I disconnect an existing database connection in Metasploit?

Use the command db_disconnect in msfconsole to disconnect the current database connection.

What is the command to reconnect Metasploit to a database?

You can reconnect using db_connect :@127.0.0.1:5432/.

Can I connect Metasploit to multiple databases simultaneously?

No, Metasploit supports only one active database connection at a time.

What should I do if Metasploit crashes while connected to a database?

Restart PostgreSQL with sudo systemctl restart postgresql, then restart Metasploit and try reconnecting.

Why does Metasploit fail to connect to the database after rebooting Kali Linux?

PostgreSQL might not be running. Start it manually with sudo systemctl start postgresql.

How do I restart Metasploit services?

Exit msfconsole and restart it using msfconsole. If needed, restart PostgreSQL with sudo systemctl restart postgresql.

How do I check if PostgreSQL is running?

Use sudo systemctl status postgresql to verify the service status.

How do I fix "No Active Database Connection" error in Metasploit?

Reconnect using db_connect or ensure PostgreSQL is running.

What does db_status return if no database is connected?

It will show [*] No Active Database Connection.

How can I make my Metasploit database connection persistent?

Configure the database.yml file to store credentials and enable auto-connection.

Where is the database.yml file located in Metasploit?

It is usually found in /usr/share/metasploit-framework/config/database.yml.

What are the correct file permissions for database.yml?

Set appropriate permissions using sudo chmod 644 /usr/share/metasploit-framework/config/database.yml.

Can I use a remote PostgreSQL database with Metasploit?

Yes, modify the database.yml file to use the remote database’s IP address.

How do I check available databases in PostgreSQL?

Run \l inside the PostgreSQL shell (psql).

How do I list all users in PostgreSQL?

Inside psql, use the command \du.

What should I do if Metasploit fails to start due to a database issue?

Restart both PostgreSQL and Metasploit, then reconfigure the database connection.

Can I use Metasploit without a database connection?

Yes, but you won’t be able to store and query scan results efficiently.

How do I create a new database for Metasploit?

Inside psql, use:

CREATE DATABASE msf_database OWNER msf_user;

How do I grant all privileges to a Metasploit database user?

Use the SQL command:

GRANT ALL PRIVILEGES ON DATABASE msf_database TO msf_user;

What is the default username for Metasploit’s database?

The default is typically msf_user, but it depends on your configuration.

How do I reset my Metasploit database connection?

Use db_disconnect, restart PostgreSQL, and then use db_connect again.

Can I use Metasploit with an encrypted database?

Yes, but additional configurations are required for encrypted PostgreSQL databases.

Why does Metasploit database connection timeout?

It may be due to PostgreSQL being inactive or a network issue. Restart PostgreSQL and reconnect.

How can I optimize Metasploit database performance?

Increase the connection pool size in database.yml and ensure PostgreSQL settings are optimized.

How do I completely reset my Metasploit database?

Drop and recreate the database in PostgreSQL:

DROP DATABASE msf_database; CREATE DATABASE msf_database OWNER msf_user;

How do I update Metasploit to fix database connection bugs?

Run sudo apt update && sudo apt upgrade metasploit-framework to update to the latest version.

What is the best way to prevent Metasploit database connection errors?

Ensure PostgreSQL starts at boot using:

sudo systemctl enable postgresql

And configure persistent connections using database.yml.

Join Our Upcoming Class! Click Here to Join
Join Our Upcoming Class! Click Here to Join