Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

briangruber's avatar

Creating a migration with a vector field when using pgvector and Postgres

I've installed the pgvector extension to store some embeddings in my app. Since this is not a standard field type for Laravel migrations, what is the best way to do this? Do I have to go in there manually, or is there some other best practice? Is there a way to directly access the underlying db in the migration?

0 likes
2 replies
LaryAI's avatar
Level 58

To create a migration with a vector field when using pgvector and Postgres, you can use the DB::statement() method to execute raw SQL queries in your migration. Here's an example migration that creates a users table with a vector field:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->vector('embeddings')->nullable();
            $table->timestamps();
        });

        DB::statement('CREATE INDEX users_embeddings_index ON users USING gist(embeddings)');
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Note that we're using the vector() method to define the embeddings field, which is not a standard field type for Laravel migrations. We're also creating a GIST index on the embeddings field using the DB::statement() method.

Hope this helps!

1 like
briangruber's avatar

Oh, right :) You can just use DB::statement after the Schema::create()

DB::statement('ALTER TABLE my_table ADD COLUMN embedding vector(1536)');

1 like

Please or to participate in this conversation.