Pgsql protocol compatibility
immudb needs to be started with the option enabled (IMMUDB_PGSQL_SERVER=true
).
SSL is supported, if you configured immudb with a certificate.
You can use a subset of the libpq (opens new window) API. You will need to include:
and compile with gcc -o main $(pkg-config libpq --cflags --libs) main.c
.
You can use the gem:
require 'pg'
Download the official JDBC driver (opens new window) jar artifact for PostgreSQL.
$ javac -cp .:./postgresql-42.2.20.jar MyProgram.java
Please refer to the documentation for instructions on how to enable it in your server.
To connect to the database:
psql "host=localhost dbname=defaultdb user=immudb password=immudb sslmode=disable"
psql (13.2, server 0.0.0)
Type "help" for help.
PGconn *conn = PQconnectdb("host=localhost user=immudb password=immudb dbname=defaultdb sslmode=disable");
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
conn = PG::Connection.open("sslmode=allow dbname=defaultdb user=immudb password=immudb host=127.0.0.1 port=5432")
It is important to pass the preferQueryMode=simple
option, as immudb pgsql server only support simple query mode.
<?php
$dbconn = pg_connect("host=localhost port=5432 sslmode=require user=immudb dbname=defaultdb password=immudb");
//...
pg_close($dbconn);
?>
Execute statements:
defaultdb=> CREATE TABLE Orders(id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id);
SELECT 1
defaultdb=> UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title1');
SELECT 1
PGresult *res = PQexec(conn, "CREATE TABLE Orders (id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id)");
do_exit(conn, res);
PQclear(res);
res = PQexec(conn, "UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title 1')");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
do_exit(conn, res);
}
PQclear(res);
conn.exec( "CREATE TABLE Orders (id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id)" )
conn.exec( "UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title 1')" )
conn.exec( "UPSERT INTO Orders (id, amount, title) VALUES (2, 400, 'title 2')" )
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE people(id INTEGER, name VARCHAR, salary INTEGER, PRIMARY KEY id);");
stmt.executeUpdate("INSERT INTO people(id, name, salary) VALUES (1, 'Joe', 20000);");
stmt.executeUpdate("INSERT INTO people(id, name, salary) VALUES (2, 'Bob', 30000);");
defaultdb=> SELECT id, amount, title FROM Orders;
(defaultdb.Orders.id) | (defaultdb.Orders.amount) | (defaultdb.Orders.title)
-----------------------+---------------------------+--------------------------
1 | 200 | "title1"
(1 row)
res = PQexec(conn, "SELECT id, amount, title FROM Orders");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printf("No data retrieved\n");
}
int rows = PQntuples(res);
for(int i=0; i<rows; i++) {
printf("%s %s %s\n", PQgetvalue(res, i, 0),
PQgetvalue(res, i, 1), PQgetvalue(res, i, 2));
}
PQclear(res);
PQfinish(conn);
conn.exec( "SELECT id, amount, title FROM Orders" ) do |result|
result.each do |row|
puts row.inspect
end
end
ResultSet rs = stmt.executeQuery("SELECT * FROM people");
while(rs.next()){
System.out.print("ID: " + rs.getInt("(defaultdb.people.id)"));
System.out.print(", Name: " + rs.getString("(defaultdb.people.name)"));
System.out.print(", Salary: " + rs.getInt("(defaultdb.people.salary)"));
System.out.println();
}
$query = 'SELECT * FROM people';
$result = pg_query($query) or die('Error message: ' . pg_last_error());
while ($row = pg_fetch_row($result)) {
}