otj-pg-embedded > Embedded PostgreSQL > ERROR: syntax error at or near "PROCEDURE"

736 Views Asked by At

I am facing an issue when I'm trying to create a PROCEDURE using EmbeddedPostgres

Rest everything is working except for when I'm trying to create a PROCEDURE Please find below the code.

EmbeddedPostgres pg = EmbeddedPostgres.builder().start();
Statement dbSetup = pg.getDatabase("postgres", "postgres").getConnection().createStatement();
dbSetup.execute("create database portal;");
dbSetup.execute("CREATE USER testadmin WITH PASSWORD 'testadmin123' CREATEDB;");
dbSetup.execute("ALTER USER testadmin WITH SUPERUSER;");
dbSetup.execute("ALTER ROLE testadmin SUPERUSER;");

String procedure = "CREATE PROCEDURE insert_data(a integer, b integer)\n" +
                   "LANGUAGE SQL\n" +
                   "AS $$\n" +
                  "INSERT INTO tbl VALUES (a);\n" +
                  "INSERT INTO tbl VALUES (b);\n" +
                  "$$;";

ResultSet rsnew = pg.getDatabase("testadmin", "portal").getConnection().createStatement().executeQuery(procedure);

Facing the below Exception

org.postgresql.util.PSQLException: ERROR: syntax error at or near "PROCEDURE"
  Position: 8
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)
    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)
    at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:224)
    at com.myproject.embeddedpostgres.TestPostgreSQL.main(TestPostgreSQL.java:62)

Can anyone please help.

0

There are 0 best solutions below