{"id":4250,"date":"2025-02-24T14:04:53","date_gmt":"2025-02-24T22:04:53","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=4250"},"modified":"2025-02-24T14:04:53","modified_gmt":"2025-02-24T22:04:53","slug":"compatibility-of-postgresql-connector-with-aws-and-gcp","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/compatibility-of-postgresql-connector-with-aws-and-gcp\/","title":{"rendered":"Compatibility of PostgreSQL Connector with AWS and GCP"},"content":{"rendered":"<p>As AI-driven applications continue to evolve, the need for efficient vector-based search capabilities is greater than ever. Microsoft Semantic Kernel makes it easy to integrate these capabilities with PostgreSQL databases using the <code>Postgres<\/code> connector. Whether you&#8217;re leveraging cloud-hosted PostgreSQL instances on Amazon Web Services or Google Cloud, this connector enables seamless interaction, allowing you to store and query vectorized data for tasks like recommendation systems, semantic search, and more.<\/p>\n<h2><strong>Compatible Databases<\/strong><\/h2>\n<p>Semantic Kernel Postgres Connector is compatible with PostgreSQL instances hosted locally or in cloud including but not limited to:<\/p>\n<ul>\n<li><a href=\"https:\/\/aws.amazon.com\/rds\/postgresql\/\">Amazon RDS for PostgreSQL<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/products\/alloydb\">Google AlloyDB for PostgreSQL<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/sql\/postgresql\">Google Cloud SQL for PostgreSQL<\/a><\/li>\n<\/ul>\n<h2><strong>Setup Instructions<\/strong><\/h2>\n<h3>Enable pgvector extension<\/h3>\n<p>Once your database is configured on AWS or GCP, enable vector similarity search by installing the <code>pgvector<\/code> extension:<\/p>\n<pre><code>CREATE EXTENSION IF NOT EXISTS vector;<\/code><\/pre>\n<h3>Install the Semantic Kernel PostgreSQL Connector<\/h3>\n<p><strong>.NET<\/strong><\/p>\n<pre><code>dotnet add package Microsoft.SemanticKernel.Connectors.Postgres --prerelease<\/code><\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre><code>pip install semantic-kernel[postgres]<\/code><\/pre>\n<h2><strong>Define Your Data Model<\/strong><\/h2>\n<p><strong>.NET<\/strong><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">using Microsoft.Extensions.VectorData;\r\n\r\npublic class Hotel\r\n{\r\n    [VectorStoreRecordKey]\r\n    public ulong HotelId { get; set; }\r\n\r\n    [VectorStoreRecordData(IsFilterable = true)]\r\n    public string HotelName { get; set; }\r\n\r\n    [VectorStoreRecordData(IsFullTextSearchable = true)]\r\n    public string Description { get; set; }\r\n\r\n    [VectorStoreRecordVector(4, DistanceFunction.CosineDistance, IndexKind.Hnsw)]\r\n    public ReadOnlyMemory? DescriptionEmbedding { get; set; }\r\n\r\n    [VectorStoreRecordData(IsFilterable = true)]\r\n    public string[] Tags { get; set; }\r\n}<\/code><\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from uuid import uuid4\r\nfrom dataclasses import dataclass, field\r\nfrom typing import Annotated\r\nfrom semantic_kernel.data import (\r\n    DistanceFunction, IndexKind, VectorStoreRecordDataField,\r\n    VectorStoreRecordKeyField, VectorStoreRecordVectorField, vectorstoremodel\r\n)\r\n\r\n@vectorstoremodel\r\n@dataclass\r\nclass Hotel:\r\n    hotel_id: Annotated[str, VectorStoreRecordKeyField()] = field(default_factory=lambda: str(uuid4()))\r\n    hotel_name: Annotated[str, VectorStoreRecordDataField(is_filterable=True)]\r\n    description: Annotated[str, VectorStoreRecordDataField(is_full_text_searchable=True)]\r\n    description_embedding: Annotated[list[float], VectorStoreRecordVectorField(dimensions=4, distance_function=DistanceFunction.COSINE, index_kind=IndexKind.HNSW)]\r\n    tags: Annotated[list[str], VectorStoreRecordDataField(is_filterable=True)]<\/code><\/pre>\n<h2><strong>Configure the Vector Store<\/strong><\/h2>\n<p><strong>.NET<\/strong><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">using Microsoft.SemanticKernel.Connectors.Postgres;\r\nusing Npgsql;\r\n\r\nNpgsqlDataSourceBuilder dataSourceBuilder = new(\"Host=&lt;endpoint_from_cloud_provider&gt;;Port=5432;Username=&lt;username&gt;;Password=&lt;password&gt;;Database=&lt;database_name&gt;;\");\r\ndataSourceBuilder.UseVector();\r\nvar dataSource = dataSourceBuilder.Build();\r\n\r\nvar vectorStore = new PostgresVectorStore(dataSource);<\/code><\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.connectors.memory.postgres import PostgresSettings, PostgresStore\r\nfrom collections.abc import AsyncGenerator\r\nfrom pydantic import SecretStr\r\n\r\nsettings = PostgresSettings(\r\n    host=\"&lt;endpoint_from_cloud_provider&gt;\",\r\n    port=5432,\r\n    user=\"&lt;username&gt;\",\r\n    password=SecretStr(\"&lt;password&gt;\"),\r\n    dbname=\"&lt;database_name&gt;\")\r\n\r\nasync def get_vector_store() -&gt; AsyncGenerator[PostgresStore, None]:\r\n    async with await settings.create_connection_pool() as pool:\r\n        yield PostgresStore(connection_pool=pool)<\/code><\/pre>\n<h2><strong>Perform a Vector Search<\/strong><\/h2>\n<p><strong>.NET<\/strong><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">\/\/ Placeholder embedding generation method.\r\nasync Task&lt;ReadOnlyMemory&gt; GenerateEmbeddingAsync(string textToVectorize)\r\n{\r\n    \/\/ your logic here\r\n}\r\n\r\nIVectorStoreRecordCollection&lt;ulong, Hotel&gt; collection = vectorStore.GetCollection&lt;ulong, Hotel&gt;(\"hotels\");\r\n\r\n\/\/ Generate a vector for your search text.\r\nReadOnlyMemory searchVector = await GenerateEmbeddingAsync(\"I'm looking for a hotel where customer happiness is the priority.\");\r\n\r\n\/\/ Perform the search.\r\nvar searchResult = await collection.VectorizedSearchAsync(searchVector, new() { Top = 1 });\r\n\r\n\/\/ Display results.\r\nawait foreach (var record in searchResult.Results)\r\n{\r\n    Console.WriteLine(\"Found hotel description: \" + record.Record.Description);\r\n}<\/code><\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.data.vector_search import VectorSearchOptions\r\n\r\nasync def vector_search():\r\n    store = await get_vector_store()\r\n    collection = store.get_collection(\"hotels\", Hotel)\r\n\r\n    # Generate a vector for your search text.\r\n    vector = await generate_vector(\"I'm looking for a hotel where customer happiness is the priority.\")\r\n    search_results = await collection.vectorized_search(\r\n        vector=vector, options=VectorSearchOptions(vector_field_name=\"vector\")\r\n    )\r\n    hotels = [record.record async for record in search_results.results]\r\n    print(f\"Found hotels: {hotels}\")<\/code><\/pre>\n<h2><strong>More information<\/strong><\/h2>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/concepts\/vector-store-connectors\">What are Semantic Kernel Vector Store connectors?<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/concepts\/vector-store-connectors\/defining-your-data-model\">Defining your data model<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/concepts\/vector-store-connectors\/vector-search\">Vector search using Semantic Kernel Vector Store connectors<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/concepts\/vector-store-connectors\/out-of-the-box-connectors\/postgres-connector\">Using the Postgres Vector Store connector<\/a><\/li>\n<\/ul>\n<h2><strong>Summary<\/strong><\/h2>\n<p>This example demonstrates how to leverage Microsoft Semantic Kernel with PostgreSQL databases on AWS and GCP to perform efficient vector searches. By setting up the <code>pgvector<\/code> extension and integrating the Semantic Kernel connector, you can store and retrieve vectorized data with ease. Whether you&#8217;re building recommendation systems, semantic search applications, or AI-powered insights, this approach ensures scalable and effective vector storage and retrieval.<\/p>\n<p>We\u2019re always interested in hearing from you. If you have feedback, questions or want to discuss further, feel free to reach out to us and the community on the<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\">\u00a0discussion boards<\/a> on GitHub! We would also love your support, if you&#8217;ve enjoyed using Semantic Kernel, give us a star on <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As AI-driven applications continue to evolve, the need for efficient vector-based search capabilities is greater than ever. Microsoft Semantic Kernel makes it easy to integrate these capabilities with PostgreSQL databases using the Postgres connector. Whether you&#8217;re leveraging cloud-hosted PostgreSQL instances on Amazon Web Services or Google Cloud, this connector enables seamless interaction, allowing you to [&hellip;]<\/p>\n","protected":false},"author":156732,"featured_media":4265,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[47,1],"tags":[48,63,9],"class_list":["post-4250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-semantic-kernel","tag-ai","tag-microsoft-semantic-kernel","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>As AI-driven applications continue to evolve, the need for efficient vector-based search capabilities is greater than ever. Microsoft Semantic Kernel makes it easy to integrate these capabilities with PostgreSQL databases using the Postgres connector. Whether you&#8217;re leveraging cloud-hosted PostgreSQL instances on Amazon Web Services or Google Cloud, this connector enables seamless interaction, allowing you to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/156732"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=4250"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/4265"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=4250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=4250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=4250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}