適用於 Visual Studio Code 的 PostgreSQL 延伸模組現在包含 GitHub Copilot 整合,使用 AI 輔助開發來增強資料庫工作流程。 一旦連線到 PostgreSQL 資料庫後,Copilot 會從即時連線存取內容資訊。 此存取權限使 @pgsql Copilot 聊天參與者能產生準確且具結構感知的 SQL 查詢與洞察,簡化開發流程並減少 Visual Studio Code 內的上下文切換。
Prerequisites
開始之前,請確認您已下載並安裝適當的工具和資源。
這些工具和資源可協助您遵循本文,並充分利用Visual Studio Code 中PostgreSQL延伸模組的 GitHub Copilot 整合。
- 安裝在您電腦上的 Visual Studio Code。
- PostgreSQL 資料庫安裝在本機或裝載於 雲端。
- Visual Studio Code 中安裝的 PostgreSQL 擴充功能。
- GitHub Copilot 擴充功能
- 已安裝 GitHub Copilot Chat 擴充功能。
- 用來連線到雲端裝載資料庫的 Azure 帳戶(選擇性)。
安裝 GitHub Copilot 和 GitHub Copilot 聊天延伸模組
如果您尚未在 Visual Studio Code 中安裝 GitHub Copilot 延伸模組:
在 Visual Studio Code 中選擇 擴充功能 圖示,搜尋 GitHub Copilot,然後選擇 安裝。
GitHub Copilot 聊天延伸模組會自動與 GitHub Copilot 一起安裝。
在 Visual Studio Code 中登入 GitHub
請確保你有 GitHub 帳號和有效的 GitHub Copilot 訂閱:
在 Visual Studio Code 中,選擇 帳戶 圖示,並選擇「 以 GitHub 登入」以使用 GitHub Copilot。
開始使用 GitHub Copilot
請遵循下列步驟,開始使用 GitHub Copilot 搭配 PostgreSQL Visual Studio Code 擴充功能。
以滑鼠右鍵按兩下 PostgreSQL 資料庫,然後選取 [與這個資料庫聊天]。
若有提示,請選擇 允許 以啟用 GitHub Copilot 存取資料庫連線上下文。
當 Copilot 聊天介面開啟時,開始提問,並使用
@pgsql前綴指定你想與 PostgreSQL 資料庫互動。
嘗試提示,例如:
@pgsql tell me about the tables in the HR schema
- Copilot 會以結構描述表格的詳細描述進行回應。
使用讀寫功能
Visual Studio Code 中 PostgreSQL 延伸模組的 GitHub Copilot 整合,可提供順暢的讀寫功能。 透過這種整合,你可以更有效率地與資料庫互動。 透過 AI 驅動的建議,你可以直接從編輯器執行查詢資料、修改結構及更新紀錄等任務。 此功能可簡化複雜的作業、減少手動工作,並提升生產力,同時維持精確度和內容感知。
Note
適用於 PostgreSQL 的 GitHub Copilot Chat 整合是一項功能強大的工具,可對您的資料庫進行變更。 使用此功能時請謹慎,尤其是在測試和生產環境中。 在執行 SQL 程式代碼之前,請務必先檢閱產生的 SQL 程式代碼,並考慮先在安全的環境中進行測試。
請嘗試更進階的提示。
@pgsql convert the hr.employees table to use a JSONB column for the address field
Copilot 可能會回應 SQL 建議,並要求許可權進行變更。
若要核准執行:
@pgsql Yes, please make the JSONB column for me
然後科皮洛特要求確認:
@pgsql Yes, I confirm
使用內容功能表選項
- 你可以在編輯器中選擇 SQL 程式碼,然後右鍵點擊 GitHub Copilot 的情境選單選項,如 「解釋查詢」、「 重寫查詢」或 「分析查詢效能」。
額外想法和快捷食譜
您可以使用 GitHub Copilot for PostgreSQL 執行許多提示和動作 - 限制只是您的想像力! 為了協助開始一些想法,以下是一些概念提示,您可以嘗試或修改以符合資料庫內容和開發環境:
查詢最佳化
利用這些提示引導 Copilot 解決特定的查詢優化挑戰。 有了 Copilot 的協助,您可以實現高效且可靠的資料庫運作。
I'm working on optimizing my database for high-concurrency workloads. The table is called transactions with millions of records, and I'm experiencing deadlocks under a heavy load. Help me optimize my table schema and queries.
I need help writing a query. The data is stored in the orders table, which uses the columns customer_id, order_date, and total_price. I also need to include a rolling 3-month average of customer spending using a window function.
I'm getting this error: 'ERROR: column `orders.total_price` must appear in the GROUP BY clause or be used in an aggregate function.
效能優化
利用這些提示引導 Copilot 解決特定的效能優化挑戰。 有了 Copilot 的協助,您可以實現更快且更有效率的資料庫操作。
Provide the Explain Plan for my most recent query, and please explain each step.
Can you run some performance metrics on my database and tell me how it performs?
My orders table has 10 million records, and queries on customer_id and order_date are slow. How can I optimize indexing, partitioning, and schema design for performance?
應用程式開發
利用這些提示引導 Copilot 解決應用程式開發的挑戰。
Generate a FastAPI endpoint to fetch orders from the ecom.orders table with pagination.
Generate an ETL pipeline script to clean and normalize the customer table data.
Generate a FastAPI project with my database using SQLAlchemy.
清除
為了確保體驗順暢,請清理你在此快速啟動期間建立的任何臨時資源或設定。 例如:
- 中斷 Visual Studio Code 中 PostgreSQL 資料庫的連線。
- 移除你在會話中建立的任何測試資料庫或資料表。
- 關閉任何開啟的連線,以避免不必要的資源使用量。
意見反應與支援
針對 Bug、功能要求和問題,請使用 Visual Studio Code 中的內建意見反應工具。 你可以透過 Visual Studio Code 說明選單或 PGSQL 指令面板完成此回饋。
說明功能表
- 移至 [說明] > [報告問題]
命令選擇區
- 使用
Ctrl + Shift + P開啟命令選擇區並執行:PGSQL: Report Issue
- 使用