Skip to content

Connect to PostgreSQL database

Connecting MindsDB to your PostgreSQL database can be done in two ways:


To connect to your PostgreSQL Server from MindsDB, you will need to install the MySQL foreign data wrapper for PostgreSQL.

How to install the MySQL foreign data wrapper

MindsDB Studio

Using MindsDB Studio, you can connect to the PostgreSQL database with a few clicks.

Connect to database

  1. From the left navigation menu, select Database Integration.
  2. Click on the ADD DATABASE button.
  3. In the Connect to Database modal:
    1. Select PostgreSQL as the Supported Database.
    2. Add the Database name.
    3. Add the Hostname.
    4. Add Port.
    5. Add PostgreSQL user.
    6. Add Password for PostgreSQL user.
    7. Click on CONNECT.

Connect to PostgreSQL

Create new Datasource

  1. Click on the NEW DATASOURCE button.
  2. In the Datasource from DB integration modal:
    1. Add Datasource Name.
    2. Add Database name.
    3. Add SELECT Query (e.g. SELECT * FROM my_database)
    4. Click on CREATE.

Create PostgreSQL Datasource

That's it🎉 🏆 💻

You have successfully connected to PostgreSQL from MindsDB Studio. The next step is to train the Machine Learning model.

PSQL client

How to extend MindsDB configuration

Our suggestion is to always use MindsDB Studio to connect MindsDB to your database. If you still want to extend the configuration without using MindsDB Studio follow configuration example.

After creating the required configuration, you will need to start MindsDB and provide the path to the newly created config.json:

python3 -m mindsdb --api=http,mysql --config=config.json

The --api parameter specifies the type of API to use, in this case HTTP and MySQL. The --config parameter specifies the location of the configuration file.

Start MindsDB with config

If MindsDB is successfully connected to your PostgreSQL database, it will create a new schema mindsdb and new table predictors. After starting the server, you can run a SELECT query from your psql-client to make sure integration has been successful.

SELECT * FROM mindsdb.predictors;

SELECT from MindsDB predictors table

That's it 🎉 🏆 💻

You have successfully connected MindsDB Server and PostgreSQL. The next step is to train the Machine Learning model.