likes
comments
collection
share

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

作者站长头像
站长
· 阅读数 92

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL

1. 概述

PostgreSQL 是一个功能强大的开源关系数据库管理系统,适用于各种应用程序。在开发过程中,调试 PostgreSQL 对于识别和解决问题至关重要。在本博客中,我们将手把手教你使用客户端计算机上的 Visual Studio Code (VSCode) 和 Linux 计算机上运行的 PostgreSQL 设置远程 PostgreSQL 开发调试环境。

2. Linux 上的设置

  • 参考此处提供的文档在物理机或虚拟机上安装 Ubuntu 22.04。并创建一个用户,例如“ubuntu”。
  • 在 Ubuntu 机器上生成 SSH 密钥对,以允许远程调试,这样就无需重复输入用户名和密码:
$ sudo apt update && sudo apt install ssh -y
$ ssh-keygen -t ed25519
$ cat /home/ubuntu/.ssh/ed25519.pub >> /home/ubuntu/.ssh/authorized_keys
  • 将私钥复制到您的 Windows 客户端计算机(例如,c:\Users\user1.ssh\id_ed25519)。注意:为了更好的安全实践,您应该在客户端机器上生成 ssh 密钥对,并将公钥信息添加到服务器的authorized_keys中,并将私钥保密为只有您自己知道的秘密。
  • 在 Ubuntu 上安装 PostgreSQL 构建依赖项:
$ sudo apt-get install -y build-essential git gdb lcov bison flex \
          libkrb5-dev libssl-dev libldap-dev libpam0g-dev python3-dev \
          tcl-dev libperl-dev gettext libxml2-dev libxslt1-dev \
          libreadline-dev libedit-dev uuid-dev libossp-uuid-dev \
          libipc-run-perl perl libtest-simple-perl
  • 克隆 PostgreSQL,编译它,然后启动 PostgreSQL 服务器:
$ git clone https://github.com/postgres/postgres.git
$ cd postgres && git checkout REL_15_3 -b pg153 
$ ./configure  --prefix=/home/ubuntu/mypg --enable-tap-tests --enable-debug CFLAGS="-g3 -O0"
$ make –j
$ make install

$ export PGDATA=/home/ubuntu/mydb
$ export PATH=/home/ubuntu/mypg/pgapp/bin:$PATH
$ export LD_LIBRARY_PATH=/home/ubuntu/mypg/pgapp/lib
$ initdb -D $PGDATA
$ pg_ctl -D $PGDATA -l logfile start
$ psql -d postgres

3. 设置 Visual Studio Code

  • 安装 Visual Studio Code 并安装“Remote – SSH”和“C/C++”调试扩展。
  • 通过输入 remote-ssh 安装“Remote – SSH”扩展,如下图所示。

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

  • 安装如下图所示的 c/c++ debugging 扩展,并选择 Microsoft 发布的扩展。

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

安装完remote-ssh和c/c++调试扩展后,你应该在左下角看到一个小图标,如下图蓝色高亮所示,

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

单击远程 ssh 连接图标并设置远程连接配置。

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

选择 Connect to Host ... 然后输入 ssh ubuntu@ubuntu-ip-addr ,如果 VS Code 要求本地 ssh 配置,则使用位于 c:\Users\user1.ssh\config 的配置并输入以下信息:

Host ubuntu-ip-addr
HostName ubuntu-ip-addr
IdentityFile c:\Users\user1.ssh\id_ed25519
IdentiftiesOnly Yes
  • 如果remote-ssh已经正确设置,那么VSCode应该弹出一个新的IDE窗口,然后使用这个新的IDE窗口从远程Ubuntu服务器打开源代码。
  • 使用以下配置在 .vscode 下创建 launch.json 文件以附加 PostgreSQL 后端:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/home/ubuntu/mypg/bin/postgres",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

确保更改 /home/ubuntu/mypg/bin/postgres 以指向 Ubuntu 服务器上的 PostgreSQL 安装路径。

  • 通过单击 View -> Run 来测试远程 PostgreSQL 调试,然后选择“gdb) Attach”并附加到 PostgreSQL 后端进程。

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

单击运行图标开始调试

使用 Visual Studio Code 在远程计算机上调试 PostgreSQL源码

选择要附加的正确 postgres 后端以进行调试

4. Summary 4. 总结

在这篇博文中,我们演示了如何使用客户端计算机上的 Visual Studio Code 和 Linux 计算机上运行的 PostgreSQL 设置远程 PostgreSQL 开发调试环境。通过此设置,开发人员可以有效地调试 PostgreSQL 并识别和解决开发过程中的问题。调试愉快!


原文地址 Debugging PostgreSQL on a remote machine with Visual Studio Code: A Step-by-Step Guide - Highgo Software Inc.


更多阅读:

转载自:https://juejin.cn/post/7327467832832442394
评论
请登录