Welcome back, fellow hackers! I recently had the Metasploit failed to connect to the Database on the ParrotSec OS version 4.2.2. I use multiple instances of ParrotSec and I had the exact same issue on all of them. I don't know if that is a thing on Kali Linux as well, but for ParrotSec the reason this happens is that PostgreSQL is not listening on port 5432, which Metasploit requires, but on port 5433. If you don't fix this, the search function of Metasploit will take very long to show results.
So what we need to do is, change the port on PostgreSQL. I'll show you how to do that.
Step 1 - Starting and Checking postgresql
First we need to make sure PostgreSQL is running.
sudo service postgresql start
Next, we are going to check which port PostgreSQL is using.
sudo ss -lntp | grep post
Now I already fixed it for my OS, but if you see port 5433 here instead of 5432, you know that you have the error I described above. Now we are going to fix that.
Step 2 - Updating the PostgreSQL config
First, run the following command to double-check the port that is being used in the config right now.
Make sure you adjust the PostgreSQL version to the actual installed version. So after typing /etc/postgresql/ hit the TAB button to see your installed version. As of October 2018, that's version 10.
sudo grep "port =" /etc/postgresql/10/main/postgresql.conf
You will probably see port = 5433 here now. As I said, I already fixed it for myself.
Now we are going to change that in the config.
You can either go ahead and use nano to edit the file:
sudo nano /etc/postgresql/10/main/postgresql.conf
Find the entry for port = 5433 and change it to 5432, hit CTRL+O to save and CTRL + X to leave, or you can just run the following command:
sudo sed -i 's/\(port = \)5433/\15432/' /etc/postgresql/10/main/postgresql.conf
As I said above, make sure you adjust the PostgreSQL version to the one you have installed (after /etc/postgresql/YOURVERSIONHERE).
Step 3 - Restarting PostgreSQL and re-initialize the Metasploit Database
As the last step, we need to restart PostgreSQL and re-initialize our Metasploit Database.
sudo service postgresql restart
After you have followed all of those steps, you should have solved the Metasploit Failed to connect to the Database error. Let me know in the comments below if that worked out for you. Want to learn more about Metasploit? Check out my other tutorials.