Table of contents :

How to (Easily) Configure PostgreSQL (AI vector search) on WordPress

Postgresql logo

Table of contents :

You can add AI-powered search to your WordPress site using WPSOLR together with PostgreSQL vector search.

PostgreSQL Vector search stack for WPSOLR

You can install our fully functional PostgreSQL vector search stack from GitHub, ready to work with WPSOLR.

Image wpsolr-stack-postgresql-vector-1024x749.png of How to (Easily) Configure PostgreSQL (AI vector search) on WordPress

⚠️ PHP ext-pgsql required

 This stack requires the PHP pgsql extension (ext-pgsql) to connect to PostgreSQL using pg_connect().

If the extension is missing, you may see errors like:

Call to undefined function pg_connect()

✅ Check if pgsql is installed

php -m | grep pgsql
You should see:
pgsql

PostgreSQL Vector Search

PostgreSQL, with the pgvector extension, can store embeddings—numerical representations of text or images produced by AI models. These embeddings allow you to perform vector-based searches, where results are matched based on meaning or similarity rather than exact keywords.

Uses semantic search: Finds results that match the meaning or context of a query. For example, searching for “something to sleep on” could return beds even if the word “sleep” isn’t in the content.

This approach enables more flexible and context-aware search within WordPress sites.

What is the pgai Extension?

pgai is a PostgreSQL extension that enables AI-powered vector search directly inside a PostgreSQL database. It integrates with PostgreSQL to store, index, and query embeddings generated by AI models. These embeddings can represent text, images, or other data in a high-dimensional vector space, allowing similarity searches that go beyond traditional keyword matching.

pgai + Ollama Integration

pgai in PostgreSQL handles vector storage and search, while Ollama provides the AI models that generate embeddings. Together, they enable AI-powered search and retrieval entirely inside a PostgreSQL workflow.

How It Works:

  1. Embedding Generation with Ollama:
    Ollama hosts or runs large language models (LLMs) or other AI models.
    When you add content (posts, pages, or images) to WordPress, Ollama generates embeddings for each piece of content.
    These embeddings are high-dimensional vectors representing the meaning or features of the content.
  2.  Storing Vectors in PostgreSQL with pgai:
    pgai takes the embeddings from Ollama and stores them in PostgreSQL tables.
    It uses vector columns (via pgai/pgvector) and supports indexing for fast similarity searches.
  3. Vector Search Queries:
    When a user performs a search, the query text can also be converted into an embedding using Ollama.
    pgai then performs a vector similarity search in PostgreSQL to find content with embeddings closest to the query.
    This can be based on cosine similarity, Euclidean distance, or inner product.
  4. Returning Results:
    Matching posts, pages, or images are returned to the user ranked by similarity rather than keyword match.
    This enables semantic search, finding content by meaning, and can also support multilingual search.

In short, Ollama generates the AI embeddings, and pgai stores and searches them efficiently in PostgreSQL. This avoids the need for an external vector database, keeping your AI search fully integrated with PostgreSQL and WordPress.

Integrate a locally installed PostgreSQL server into your WordPress

1. Install PostgreSQL

We are going to use docker containers to host the different applications. To install the docker engine, use the following link : https://docs.docker.com/engine/install/.

Also, don’t forget to install docker-compose.

For more detailed PostgreSQL installation and configuration instructions, you could check out our guide.

Create the following “docker-compose.yml” file :

services:
  pgdb:
    container_name: postgresql # hostname in WPSOLR config
    image: timescale/timescaledb-ha:pg18-oss
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: wpsolr # create database 'wpsolr' if not initialized yet
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - wpsolr-bridge

  pgadmin4:
    image: dpage/pgadmin4
    container_name: pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "8080:80"
    depends_on:
      - pgdb
    networks:
      - wpsolr-bridge

  pgai:
    image: timescale/pgai-vectorizer-worker:latest
    depends_on:
      - pgdb
    environment:
      PGAI_VECTORIZER_WORKER_DB_URL: postgres://postgres:postgres@pgdb:5432/wpsolr
      PGAI_VECTORIZER_WORKER_POLL_INTERVAL: "5s"  # <--- Check every 5 seconds to embed changed rows with Ollama's embedding model # OpenAI (api key required, no url required, text-embedding-3-small, 1536) OPENAI_API_KEY: "sk-tCwmmhATGpO4Bsnedoc1KRx2faVQXGknCFHruaNy" # Cohere (api key required, no url required, embed-english-v3.0, 1024) COHERE_API_KEY: "vjviOfX0wUfBBn7ae7sTaUWtKPZQKogyxtufKJ60" # vLLM (api key required, https://vllm_server:8000/v1, nnomic-ai/nomic-embed-text-v1.5, 768) VLLM_API_KEY: "my-vllm-api-key" # Infinity (api key required, https://infinity_server:7997/v1, nomic-ai/nomic-embed-text-v1.5, 768) INFINITY_API_KEY: "my-infinity-api-key" # Ollama (no api key, https://ollama:11434, nomic-embed-text, 768) networks: - wpsolr-bridge pgai-init: image: timescale/pgai-vectorizer-worker:latest depends_on: - pgdb entrypoint: >
      bash -c "
        python -m pgai install -d $$PGAI_VECTORIZER_WORKER_DB_URL
      "
    environment:
      PGAI_VECTORIZER_WORKER_DB_URL: postgres://postgres:postgres@pgdb:5432/wpsolr
    networks:
      - wpsolr-bridge

  ollama:
    container_name: ollama
    image: ollama/ollama
    networks:
      - wpsolr-bridge

 

2. Add locally hosted PostgreSQL with Timescale extensions to your WordPress using WPSolr Free

WPSolr is a WordPress search plugin that powers up WordPress search using fast, accurate and scalable search engines. This means it can integrate PostgreSQL into any WordPress website.

Since this is a locally installed PostgreSQL server, you can use the free version : WPSolr Free.

You can search for WPSolr in the WordPress plugins catalogue.

Activate WPSolr Free in the "Add Plugins page" page

Click on the install and activate buttons for the WPSolr plugin.

Click on the "WPSolr Free" button to access the settings

Once you have installed and activated the WPSolr Free plugin, you can configure it.

To do that you first need to open the WPSolr settings page.

Open the indexes page in WPSolr dashboard.

Create the PostgreSQL index

To add your WordPress data to your PostgreSQL server, you first need to create an index. You can do that directly from WPSolr.

This portion will teach you how to create your index on your locally installed PostgreSQL server.

Open the indexes tab in the WPSolr dashboard.

Create a new Elasticsearch index in the WPSolr dashboard.

Create a new index.

Image wpsolr-new-postgresql-index-scaled.png of How to (Easily) Configure PostgreSQL (AI vector search) on WordPress

Create the index by doing the following :

  • Select PostgreSQL search engine.
  • Enter the name of the index that will be displayed in the WPSolr settings.
  • Set the name of the index in the PostgreSQL server (it will create a schema in database ‘wpsolr’, with a data table, an ambedding table, an Ollama vectorize, and HSNW index)
  • Set the database host (container name in the docker compose file, or host name) and database port
  • Set the database user/password (same as in the docker compose file)
  • Choose the LLM inference provider, here Ollama
  • Set the Ollama host url :
    • if the WordPress server is a container in the same docker network as PostgreSQL, you need to enter the name of the service (the one in the docker-compose.yml file). For this example, it should be “https://ollama:11434”.
    • if the WordPress is on a separate server than PostgreSQL, you need to enter the ip address or hostname of the Ollama server.
  • Set the embedding model name
  • Set the embedding model dimensions

You can now save your index.

Index your data

Once you have created your index, you can click on “2. Define your search” tab.

 

WPSolr open data tab

Click on the “2.2 Data” tab.

WPSolr select data to index

Select the data types you want to index. You can do that by simply checking the boxes.

Open "Send your data"

Click on the “3. Send your data” tab.

Image wpsolr-new-postgresql-indexing-scaled.png of How to (Easily) Configure PostgreSQL (AI vector search) on WordPress

 

 

Check the boxes of the data you selected previously. You can then click on the “Index selected post types” button to index the data.

Configure your search

This section will focus on replacing the WordPress search with WPSolr search powered by your PostgreSQL server.

Click on the “2. Define your search” tab.

Select the Weaviate index from the drop--down list.

 

Select the index you created previously. In this example, “My local PostgreSQL index”.

Image wpsolr-postgresql-replace-archives-scaled.png of How to (Easily) Configure PostgreSQL (AI vector search) on WordPress

 

Select the archives to replace with WPSolr search. This means that the filtering sorting, etc… of content in the search pages and homepage will be handled by WPSolr and PostgreSQL.

Since you have selected “Search archive”, it should now be used whenever your use a search bar.

3. Add Facets and Ajax search to your WordPress

WPSolr Free can also add Ajax (live) search and facets powered by PostgreSQL to your WordPress websites.

Learn how to add ajax search using WPSolr.

Learn how to add facets using WPSolr.

 

Conclusion

It is a good idea to integrate search engines like PostgreSQL into WordPress have a AI search. You can do precisely that using WPSolr which can integrate a host of search engines into your WordPress and Woocommerce websites.

Trending posts
You might also like