microestc

Postgresql 使用Vscode开发指南

depends libraries

sudo apt install -y libsystemd-dev libxml2-dev libssl-dev libicu-dev zlib1g-dev libreadline-dev pkg-config

编译安装

adduser postgres

<!-- cd postgresql目录 -->
mkdir build
mkdir pgsql
cd build
<!-- --prefix=绝对路径 -->
/configure --prefix=/home/postgres/repos/postgresql-15.1/pgsql --with-icu --with-openssl --with-systemd --with-libxml --enable-debug
make -j12
make install
cd ..
mkdir -p pgsql/data
chown postgres pgsql/data
su - postgres
pgsql/bin/initdb -D pgsql/data

前台运行

pgsql/bin/postgres -D pgsql/data

后台运行

pgsql/bin/pg_ctl -D pgsql/data -l logfile start

命令交互工具

pgsql/bin/psql

Vscode 配置文件

.vscode/launch.json

{
    "configurations": [
        {
            "name": "Debug PG SRV",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/pgsql/bin/postgres",
            "args": [
                "-D",
                "pgsql/data"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "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
                },
                {
                    "text": "-gdb-set follow-fork-mode child",
                    "ignoreFailures": true
                },
                {
                    "text": "-gdb-set detach-on-fork on",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "rebuild_db",
            "miDebuggerPath": "/usr/bin/gdb"
        },
        {
            "name": "Attach PG SRV",
            "type": "cppdbg",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "program": "${workspaceFolder}/pgsql/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
                }
            ]
        },
    ],
    "version": "2.0.0"
}

.vscode/tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "install_depends",
            "command": "sudo apt install -y libsystemd-dev libxml2-dev libssl-dev libicu-dev zlib1g-dev libreadline-dev pkg-config",
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "detail": "Task install depends."
        },
        {
            "type": "shell",
            "label": "build_env",
            "command": "mkdir build && mkdir -p pgsql/data",
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "detail": "Task add folders."
        },
        {
            "type": "shell",
            "label": "build_config",
            "command": "../configure",
            "args": [
                "--prefix=${workspaceFolder}/pgsql",
                "--with-icu",
                "--with-openssl",
                "--with-systemd",
                "--with-libxml",
                "--enable-debug"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "detail": "Task Build MakeFile."
        },
        {
            "type": "shell",
            "label": "make",
            "command": "make",
            "args": [
                "-j12"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "detail": "Task build."
        },
        {
            "type": "shell",
            "label": "make_install",
            "command": "make",
            "args": [
                "install"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "detail": "Task install database."
        },
        {
            "type": "shell",
            "label": "init_db",
            "command": "pgsql/bin/initdb",
            "args": [
                "-D",
                "pgsql/data"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "detail": "Task init default database."
        },
        {
            "type": "shell",
            "label": "clean_db",
            "command": "make uninstall && make clean && rm -rf ../pgsql && rm -rf ../build",
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "detail": "Task clean database."
        },
        {
            "type": "shell",
            "label": "build_db_conf",
            "dependsOn": [
                "build_env",
                "build_config"
            ],
            "dependsOrder": "sequence",
            "detail": "Task add folders."
        },
        {
            "type": "shell",
            "label": "build_db",
            "dependsOn": [
                "make",
                "make_install",
                "init_db"
            ],
            "dependsOrder": "sequence",
            "detail": "Task add folders."
        },
        {
            "type": "shell",
            "label": "rebuild_db",
            "dependsOn": [
                "make",
                "make_install"
            ],
            "dependsOrder": "sequence",
            "detail": "Task add folders."
        },
    ],
    "version": "2.0.0"
}

安装目录修改必要的配置

pgsql/data/postgresql.conf

listen_addresses = '*'

pgsql/data/pg_hba.conf

host    all             all             0.0.0.0/0               md5

Vscode 调试操作

F1 选择 “Tasks:Run Task” 继续选择 “build_db_conf”
F1 选择 “Tasks:Run Task” 继续选择 “build_db”

DEBUG 工具栏选择 Debug PG SRV,F5 开始调试postgres主进程

如果要调试其它进程,比如我开启psql交互工具建立一个连接,ps aux 可以看到新增的进程信息,DEBUG 工具栏继续选择 Attach PG SRV,F5 开启调试选择需要调试的进程ID

注意:Vscode 需要使用 postgres 用户登录才行(非ROOT用户,其它用户也行,需要新建用户,需要额外的操作)。

相关文章: