你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:在 Visual Studio Code 预览版中配置 GitHub Copilot for PostgreSQL 扩展

Visual Studio Code 的 PostgreSQL 扩展现在包括 GitHub Copilot 集成,通过 AI 辅助开发增强数据库工作流。 一旦连接到 PostgreSQL 数据库,Copilot 就可以从实时连接中访问上下文信息。 这使 Copilot 聊天参与者能够 @pgsql 生成准确的架构感知 SQL 查询和见解,简化开发和最小化 Visual Studio Code 中的上下文切换。

先决条件

在开始之前,请验证是否已下载并安装正确的工具和资源。

这些工具和资源可帮助你遵循本文,充分利用 Visual Studio Code 中 PostgreSQL 扩展的 GitHub Copilot 集成。

安装 GitHub Copilot 和 GitHub Copilot 聊天扩展

  1. 如果尚未在 Visual Studio Code 中安装 GitHub Copilot 扩展:

  2. 在 Visual Studio Code 中选择 “扩展 ”图标,搜索 GitHub Copilot,然后选择“ 安装”。

  3. GitHub Copilot Chat 扩展随 GitHub Copilot 一起自动安装。

在 Visual Studio Code 中登录到 GitHub

  1. 确保拥有 GitHub 帐户和有效的 GitHub Copilot 订阅:

  2. 在 Visual Studio Code 中,选择 “帐户” 图标,然后选择 “使用 GitHub 登录”以使用 GitHub Copilot

开始使用 GitHub Copilot

按照步骤开始使用 GitHub Copilot 和 PostgreSQL Visual Studio Code 扩展。

  1. 右键单击 PostgreSQL 数据库,然后选择 “使用此数据库聊天”。

  2. 如果出现提示,请选择 “允许” 以允许 GitHub Copilot 访问数据库连接上下文。

  3. 当 Copilot 聊天界面打开时,可以使用前缀开始提问 @pgsql ,以指定要与 PostgreSQL 数据库交互。

尝试以下提示:

@pgsql tell me about the tables in the HR schema

Copilot 对话助手响应的屏幕截图,其中显示了 PostgreSQL 数据库的 HR 架构中表和列的详细细目。

  1. Copilot 使用架构表的详细说明进行响应。

使用读/写功能

Visual Studio Code 中 PostgreSQL 扩展的 GitHub Copilot 集成可实现无缝读取和写入功能,使开发人员能够更有效地与其数据库进行交互。 利用 AI 支持的建议,可以直接从编辑器执行查询数据、修改架构和更新记录等任务。 此功能简化了复杂作,减少了手动工作量,并提高了工作效率,同时保持准确性和上下文感知。

注释

适用于 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 解决特定的查询优化难题,帮助你实现高效可靠的数据库作。

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 解决特定的性能优化难题,帮助实现更快、更高效的数据库作。

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 中的内置反馈工具。 可以通过 VS Code 帮助菜单或 PGSQL 命令面板完成此作。

  • “帮助”菜单

    • 转到“帮助”>“报告问题”
  • 命令面板

    • 请通过 Ctrl + Shift + P 打开命令面板,并运行:PGSQL: Report Issue