Charith H
Charith H

Reputation: 355

Ballerina: How to connect PostgreSQL database with Ballerina App?

I am new to Ballerina and trying to understand the basic usage and features. Is there any easy way to connect PostgreSQL Database with Ballerina App?

Upvotes: 1

Views: 423

Answers (1)

Manuri Perera
Manuri Perera

Reputation: 534

You can do this using ballerina jdbc package. Also note that you will have to copy the postgres jdbc driver to ${BALLERINA_HOME}/bre/lib.

Following is a sample for connecting to a postgres database and performing a select operation.

import ballerina/jdbc;
import ballerina/io;

endpoint jdbc:Client testDB {
        url: "jdbc:postgresql://localhost:5432/testdb3",
        username: "postgres",
        password: "123",
        poolOptions: { maximumPoolSize: 1 }
    };

type Customer record {
    int id,
    string name,
};

function main(string... args) {

    table dt = check testDB->select("select id, name from Customers", Customer);

    while (dt.hasNext()) {
        Customer rs = check <Customer>dt.getNext();
        io:println(rs.id);
        io:println(rs.name);
    }
    testDB.stop();
}

Please refer this for a complete example of the capabilities of ballerina jdbc package.

Upvotes: 2

Related Questions