Using JavaScript in PG


Using JavaScript in PG requires plv8 extension

Extension needs to be turned on

create extension plv8;

Create function template

create or replace function function_name() 
returns void as $$
    // V8 Javascript
    //Write JavaScript code here
$$ language plv8;

Run if executed

select function_name();

Plv8 HelloWorld example

create or replace function hello_world(name text) 
returns text as $$

    let output = `Hello, ${name}!`;
    return output;

$$ language plv8;

Execute SQL usingplv8.executefunction

create or replace function update_user(id bigint, first_name text) 
returns smallint as $$

    var num_affected = plv8.execute(
        'update profiles set first_name = $1 where id = $2', 
        [first_name, id]

    return num_affected;
$$ language plv8;


Recommended Today


Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]