Pgsql protocol compatibility

immudb can talk the pgsql wire protocolPgsql protocol compatibility - 图1 (opens new window) which makes it compatible with a widely available set of clients and drivers.

Note: immudb supports the pgsql wire protocol. It is not compatible with the SQL dialect. Check the immudb SQL reference to see what queries and operations are supported.

Some pgsql clients and browser application execute incompatible statements in the background or directly query the pgsql catalog. Those may not work with immudb.

immudb needs to be started with the pgsql-server option enabled (IMMUDB_PGSQL_SERVER=true).

SSL is supported, if you configured immudb with a certificate.

Use the psql clientPgsql protocol compatibility - 图2 (opens new window) included with PostgreSQL.

You can use a subset of the libpqPgsql protocol compatibility - 图3 (opens new window) API. You will need to include:

  1. #include <libpq-fe.h>

and compile with gcc -o main $(pkg-config libpq --cflags --libs) main.c.

You can use the pgPgsql protocol compatibility - 图4 (opens new window) gem:

  1. require 'pg'

Download the official JDBC driverPgsql protocol compatibility - 图5 (opens new window) jar artifact for PostgreSQL.

You can then compile your program:

  1. $ javac -cp .:./postgresql-42.2.20.jar MyProgram.java

Please refer to the PHP pgsql modulePgsql protocol compatibility - 图6 (opens new window) documentation for instructions on how to enable it in your server.

To connect to the database:

  1. psql "host=localhost dbname=defaultdb user=immudb password=immudb sslmode=disable"
  2. psql (13.2, server 0.0.0)
  3. Type "help" for help.
  1. PGconn *conn = PQconnectdb("host=localhost user=immudb password=immudb dbname=defaultdb sslmode=disable");
  2. if (PQstatus(conn) == CONNECTION_BAD) {
  3. fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
  4. PQfinish(conn);
  5. exit(1);
  6. }
  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.

  1. Connection conn =
  2. DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/defaultdb?sslmode=allow&preferQueryMode=simple",
  3. "immudb", "immudb");
  4. System.out.println("Opened database successfully");
  1. <?php
  2. $dbconn = pg_connect("host=localhost port=5432 sslmode=require user=immudb dbname=defaultdb password=immudb");
  3. //...
  4. pg_close($dbconn);
  5. ?>

Execute statements:

  1. defaultdb=> CREATE TABLE Orders(id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id);
  2. SELECT 1
  3. defaultdb=> UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title1');
  4. SELECT 1
  1. PGresult *res = PQexec(conn, "CREATE TABLE Orders (id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id)");
  2. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  3. do_exit(conn, res);
  4. }
  5. PQclear(res);
  6. res = PQexec(conn, "UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title 1')");
  7. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  8. do_exit(conn, res);
  9. }
  10. PQclear(res);
  1. conn.exec( "CREATE TABLE Orders (id INTEGER, amount INTEGER, title VARCHAR, PRIMARY KEY id)" )
  2. conn.exec( "UPSERT INTO Orders (id, amount, title) VALUES (1, 200, 'title 1')" )
  3. conn.exec( "UPSERT INTO Orders (id, amount, title) VALUES (2, 400, 'title 2')" )
  1. Statement stmt = conn.createStatement();
  2. stmt.executeUpdate("CREATE TABLE people(id INTEGER, name VARCHAR, salary INTEGER, PRIMARY KEY id);");
  3. stmt.executeUpdate("INSERT INTO people(id, name, salary) VALUES (1, 'Joe', 20000);");
  4. stmt.executeUpdate("INSERT INTO people(id, name, salary) VALUES (2, 'Bob', 30000);");
  1. $stmt = 'CREATE TABLE people(id INTEGER, name VARCHAR, salary INTEGER, PRIMARY KEY id);';
  2. $result = pg_query($stmt) or die('Error message: ' . pg_last_error());
  3. $stmt = 'INSERT INTO people(id, name, salary) VALUES (1, 'Joe', 20000);';
  4. $result = pg_query($stmt) or die('Error message: ' . pg_last_error());
  5. $stmt = 'INSERT INTO people(id, name, salary) VALUES (2, 'Bob', 30000);';

Query and iterate over results:

  1. defaultdb=> SELECT id, amount, title FROM Orders;
  2. (defaultdb.Orders.id) | (defaultdb.Orders.amount) | (defaultdb.Orders.title)
  3. -----------------------+---------------------------+--------------------------
  4. 1 | 200 | "title1"
  5. (1 row)
  1. res = PQexec(conn, "SELECT id, amount, title FROM Orders");
  2. if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  3. printf("No data retrieved\n");
  4. PQclear(res);
  5. do_exit(conn, res);
  6. }
  7. int rows = PQntuples(res);
  8. for(int i=0; i<rows; i++) {
  9. printf("%s %s %s\n", PQgetvalue(res, i, 0),
  10. PQgetvalue(res, i, 1), PQgetvalue(res, i, 2));
  11. }
  12. PQclear(res);
  13. PQfinish(conn);
  1. conn.exec( "SELECT id, amount, title FROM Orders" ) do |result|
  2. result.each do |row|
  3. puts row.inspect
  4. end
  5. end
  1. ResultSet rs = stmt.executeQuery("SELECT * FROM people");
  2. while(rs.next()){
  3. System.out.print("ID: " + rs.getInt("(defaultdb.people.id)"));
  4. System.out.print(", Name: " + rs.getString("(defaultdb.people.name)"));
  5. System.out.print(", Salary: " + rs.getInt("(defaultdb.people.salary)"));
  6. System.out.println();
  7. }
  1. $query = 'SELECT * FROM people';
  2. $result = pg_query($query) or die('Error message: ' . pg_last_error());
  3. while ($row = pg_fetch_row($result)) {
  4. var_dump($row);
  5. }