Postgres
postgres
(postgres).PGClient
PGClient is a client for Postgres database. Internally client uses go-pg/pg driver.
Kind: inner class of postgres
pgClient.Connect(host, port, username, password) ⇒ bool
Connect connects to Postgres database using given credentials. The connection is closed after the function returns.
Kind: instance method of PGClient
Returns: bool
- - If connection is successful, it returns true.
Throws:
error
- If connection is unsuccessful, it returns the error.
Param | Type | Description |
---|---|---|
host | string | The host of the Postgres database. |
port | int | The port of the Postgres database. |
username | string | The username to connect to the Postgres database. |
password | string | The password to connect to the Postgres database. |
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isConnected = c.Connect('localhost', 5432, 'username', 'password');
pgClient.ConnectWithDB(host, port, username, password, dbName) ⇒ bool
ConnectWithDB connects to Postgres database using given credentials and database name. The connection is closed after the function returns.
Kind: instance method of PGClient
Returns: bool
- - If connection is successful, it returns true.
Throws:
error
- If connection is unsuccessful, it returns the error.
Param | Type | Description |
---|---|---|
host | string | The host of the Postgres database. |
port | int | The port of the Postgres database. |
username | string | The username to connect to the Postgres database. |
password | string | The password to connect to the Postgres database. |
dbName | string | The name of the database to connect to. |
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isConnected = c.ConnectWithDB('localhost', 5432, 'username', 'password', 'mydb');
pgClient.ExecuteQuery(host, port, username, password, dbName, query) ⇒ string
ExecuteQuery connects to Postgres database using given credentials and database name and executes a query on the db.
Kind: instance method of PGClient
Returns: string
- - The result of the query execution.
Throws:
error
- If query execution is unsuccessful, it returns the error.
Param | Type | Description |
---|---|---|
host | string | The host of the Postgres database. |
port | int | The port of the Postgres database. |
username | string | The username to connect to the Postgres database. |
password | string | The password to connect to the Postgres database. |
dbName | string | The name of the database to connect to. |
query | string | The query to execute on the database. |
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let result = c.ExecuteQuery('localhost', 5432, 'username', 'password', 'mydb', 'SELECT * FROM users');
pgClient.IsPostgres(host, port) ⇒ bool
IsPostgres checks if the given host and port are running Postgres database.
Kind: instance method of PGClient
Returns: bool
- - If the host and port are running Postgres database, it returns true.
Throws:
error
- If the check is unsuccessful, it returns the error.
Param | Type | Description |
---|---|---|
host | string | The host to check. |
port | int | The port to check. |
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isPostgres = c.IsPostgres('localhost', 5432);