DEV Community

Yuya Shinde
Yuya Shinde

Posted on

1

Using VS Code Tasks for PostgreSQL Development

This post builds upon the content presented in our previous two articles, providing further guidance with additional configurations.

  1. Building and Running PostgreSQL from Source Code
  2. Debugging PostgreSQL with GDB + Visual Studio Code
  3. Setting up IntelliSense for PostgreSQL Development

When developing with PostgreSQL, it's often necessary to perform various operations such as compiling, installing, initializing the database, starting and stopping the server. In this article, we'll show you how to add these tasks to Visual Studio Code (VS Code) using the Task feature.

What are Tasks in VS Code?

Tasks in VS Code allow you to automate build scripts and external operations by creating a configuration file called tasks.json. This file is located in the .vscode directory of your workspace. By defining tasks, you can easily run them from within VS Code, making it easier to manage your development workflow.

Creating Tasks for PostgreSQL Development

To create tasks for PostgreSQL development, we'll need to add a new configuration file named tasks.json to our workspace. Here's an example of what the file might look like:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Configure",
            "type": "shell",
            "command": "./configure",
            "args": [
                "--prefix=${PG_BUILD_DIR}",
                "--enable-depend",
                "--enable-debug",
                "--enable-cassert",
                "--enable-tap-tests",
                "CFLAGS=-O0"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }        
        },
        {
            "label": "Build",
            "type": "shell",
            "command": "make",
            "args": [],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "dependsOn": [
            ],
            "problemMatcher": [],
            "hide": true
        },
        {
            "label": "Build & Install",
            "type": "shell",
            "command": "make",
            "args": [
                "install"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": [
                "Build"
            ],
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }        
        },
        {
            "label": "Clean",
            "type": "shell",
            "command": "make",
            "args": [
                "clean"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }
        },
        {
            "label": "CleanDataFolder",
            "type": "shell",
            "command": "rm",
            "args": [
                "-rf", "${PG_BUILD_DIR}/data/*"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "build",
                "isDefault": false
            },
            "problemMatcher": [],
            "hide": true
        },
        {
            "label": "CreateDataFolder",
            "type": "shell",
            "command": "mkdir -p ${PG_BUILD_DIR}/data",
            "args": [
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "dependsOn": [
            ],
            "problemMatcher": [],
            "hide": true
        },
        {
            "label": "InitDB",
            "type": "shell",
            "command": "${PG_BUILD_DIR}/bin/initdb -D ${PG_BUILD_DIR}/data",
            "args": [
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "dependsOn": [
            ],
            "problemMatcher": [],
            "hide": true
        },
        {
            "label": "Init & Run",
            "type": "shell",
            "command": "sudo",
            "args": [
                "-u", "${USER}",
                "${PG_BUILD_DIR}/bin/pg_ctl",
                "-D", "${PG_BUILD_DIR}/data",
                "-l", "${PG_BUILD_DIR}/data/logfile",
                "start"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "dependsOn": [
                "CleanDataFolder",
                "CreateDataFolder", 
                "InitDB",
            ],
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }        
        },
        {
            "label": "Run",
            "type": "shell",
            "command": "sudo",
            "args": [
                "-u", "${USER}",
                "${PG_BUILD_DIR}/bin/pg_ctl",
                "-D", "${PG_BUILD_DIR}/data",
                "-l", "${PG_BUILD_DIR}/data/logfile",
                "start"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "dependsOn": [
            ],
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }        
        },
        {
            "label": "Stop",
            "type": "shell",
            "command": "sudo",
            "args": [
                "-u", "${USER}",
                "${PG_BUILD_DIR}/bin/pg_ctl", "stop",
                "-D", "${PG_BUILD_DIR}/data",
                "-m", "smart"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "env": {
                    "PG_BUILD_DIR": "${workspaceFolder}/../build/pgsql/master"
                }
            },
            "group": {
                "kind": "test",
                "isDefault": false
            },
            "dependsOn": [],
            "problemMatcher": [],
            "presentation": {
                "reveal": "never",
                "revealProblems": "onProblem",
                "showReuseMessage": false,
                "panel": "shared"
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined several tasks for PostgreSQL development, including:

  • Configure: runs the ./configure script with various options
  • Build & Install: runs both make and make install
  • Clean: runs make clean to delete built objects
  • Init & Run: initializes the database and starts the server
  • Run: starts the server
  • Stop: stops the server

Each task has a unique label, type, command, and arguments. Details of each option are described on the following page. The JSON schema definition is also useful to understand what you can set in the each attribute.

Running Tasks in VS Code

To run a task in VS Code, follow these steps:

  1. Open the Command Palette by pressing Cmd+Shift+P (Mac)
  2. Type "Task: Run Task" and select the task you want to run
  3. Press Enter to run the task

Alternatively, you can also run tasks from the Terminal in VS Code by typing task .

By adding these tasks to your VS Code configuration, you'll be able to easily manage your PostgreSQL development workflow and automate repetitive tasks.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more