Set search_path with H2 and Liquibase - postgresql

I have an issue with my project.
I'm using liquibase ( v4.8.0 ) and on the changelog-master.xml I include a SQL file taht contains that code :
CREATE SCHEMA IF NOT EXISTS public;
SET search_path TO public;
When I run my app with PostgreSQL it works fine, but when I run my tests that are using a H2 database ( h2 v2.1.210 ) it fails with that error :
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'liquibase' defined in class path
resource
[org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]:
Invocation of init method failed; nested exception is
liquibase.exception.LiquibaseException:
liquibase.exception.MigrationFailedException: Migration failed for
change set
db/schema.sql::raw::includeAll: Syntax
error in SQL statement "SET [*]search_path TO public;"; expected "#,
AUTOCOMMIT, EXCLUSIVE, IGNORECASE, PASSWORD, SALT, MODE, DATABASE,
COLLATION, CLUSTER, DATABASE_EVENT_LISTENER, ALLOW_LITERALS,
DEFAULT_TABLE_TYPE, SCHEMA, CATALOG, SCHEMA_SEARCH_PATH,
JAVA_OBJECT_SERIALIZER, IGNORE_CATALOGS, SESSION, TRANSACTION, TIME,
NON_KEYWORDS, DEFAULT_NULL_ORDERING, LOG"; SQL statement: SET
search_path TO public; [42001-210] [Failed SQL: (42001) CREATE SCHEMA
IF NOT EXISTS public;
I don't understand why it's failing and also I didn't find any help on the web.
Thank you in advance for your help :)

The native syntax for H2 is SET SCHEMA_SEARCH_PATH schemaName, …. This syntax is available unconditionally.
When PostgreSQL compatibility mode is enabled, H2 additionally provides SET SEARCH_PATH command with specialized implementation, it is different from SET SCHEMA_SEARCH_PATH, because it adds pg_catalog first if it wasn't specified and it supports $user. To enable this compatibility mode you need to append ;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH to JDBC connection URL. It is enough to add ;MODE=PostgreSQL to make SET SEARCH_PATH available, but some other deviations between H2 and PostgreSQL need to be enabled separately, you may need them too. See documentation of compatibility modes for mode details:
https://h2database.com/html/features.html#compatibility
This information is actual for the new versions of H2. Old unsupported versions have different options.

Related

Creating a new Postgres Scheme for Offer-Ready in Azure Cloud - "Non empty schema"

I tried to build an Offer-Ready Docker container on Azure Cloud. Although I created a new (blank) table in PostgreSQL, I got this strange error message.
javax.servlet.ServletException: org.eclipse.jetty.servlet.ServletHolder$1: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
I double-checked the database, there is no table in schema "public". I didn't have that problem on AWS. Has anybody an idea what is different on Azure?
I had the same experience once.
The PostgreSQL database on Azure seemed empty (\dt returned no results),
But Flyway claimed the database was not empty (and therefore would not apply the migration scripts, for fear of interfering with whatever was already there).
Here is what I did was:
Create a new schema within the database e.g. myschema
Delete the default schema called public
Add the parameter currentSchema=myschema to the JDBC URL
And then it worked. I never got to find out what the root cause of this problem was.
EDIT: This link might provide more information on what objects are in the "public" schema by default on Azure PostgreSQL: https://community.atlassian.com/t5/Jira-questions/Re-quot-database-that-is-not-empty-quot-when-trying-to-use-azure/qaq-p/1308795/comment-id/410329#M410329

Can't install extensions per schema as it claims that it exists in there (it doesn't). How to force it any further?

I am trying to use dblink, I have thousands of schemas.
I have tried to install dblink extension using:
create extension dblink schema exampleschema;
Of course I am getting this error:
Error occurred during SQL query execution
Reason:
SQL Error [42710]: ERROR: extension "dblink" already exists
This is not true as I cannot use dblink for specific schema (I am accessing it through the app that I am the dev) as it cannot find it.
Only way to make dblink work is to use set search to a schema were extensions I've managed to install.
I've tried to set search path to a schema where extension is missing
Tried to use above query
Tried to set connection active to selected schema
Can't figure out what I would be able to use...
Extension was only created for public and schema1, however everytime I try to install extension to any other schema I will get information that extension is missing (extensions is not found when using query (when set search path to proper schema) or checking in system info / extensions).
How can I force postgresql to install extension in specific schema ? Is there any other query as obviously above query doesn't work (extensions doesn't exist in exampleschema)
An extension is not installed per schema, but per database.
Extension was only created for public and schema1
You are misunderstanding how an extension is installed.
An extension is installed in one (and only one) schema, which means that all functions that the extension provides are created in that schema. So if you installed the dblink extension in the schema dblink_schema all dblink functions are stored there.
To make that extension available to users that have a different search_path, you need to change that user's search_path to include the schema where the extension is installed in (not to the schema where it's missing), e.g.:
alter user app_user
set search_path = public,dblink_schema;

Connection validation error using postgresql jdbc 4.2 driver against a 9.3 database -- apparent case-sensitivity of SEARCH_PATH keyword

Using the jdbc4.2 implementation contained in postgresql-9.4.1212.jar, I generate an error when calling the java.Sql.Connection isValid() method on a connection to a postgresql 9.3 database (java8 and postgres both running on windows 7).
The path to producing the error is complicated but reproducible (will provide relevant code shortly) and involves a sequence of sql calls on a single db connection whose default schema is reconfigured prior to each use via an explicit execution of SET SEARCH_PATH='[some schema]'.
I find that the error occurs if and only if I render the SEARCH_PATH keyword using upper case (that is, the error does not occur if I execute SET search_path='[some schema]' - only when I execute SET SEARCH_PATH='[some schema]').
Note that the direct effect of executing either variant is the same -- in both cases the default schema associated with the connection is changed to [some schema]. It's just that, eventually, a downstream call to java.sql.connection.isValid() causes the database to crash if I've used SEARCH_PATH instead of search_path.
I can see that the jdbc driver's implementation of java.sql.connection.setSchema() uses the lower-case variant; something that makes me think this apparent case-sensitivity may be a known issue, but I have found no mentions of it anywhere online.
Note that the problem does not occur if I either: (1) use an older jdbc driver (postgresql-9.3.1100.jdbc4.1.jar) with my 9.3 database, or (2) use the latest jdbc driver with a postgresql 9.6 database.
I'm wondering if anyone has run into this specific problem, and also, if there are other known incompatibilities b/w the 9.3 database and the latest jdbc driver.
The driver is failing to invalidate the prepared statement cache because it only detects SET search_path=... when the config parameter is lower case.
See line 2056 of this commit.
I can't find an issue that describes this. Please have a look yourself and raise one if needed.

PhpStorm - ERROR: type "hstore" does not exist

I use primary pgAdmin to browse and edit my PostgreSQL database. Now I would like to use PhpStorm.
In PhpStorm I can browse my tables, but I can not edit data. When I try I get error:
[42704] ERROR: type "hstore" does not exist
Kde: compilation of PL/pgSQL function "on_update" near line 3
Function on_update is on update trigger and it save old row to history table and it uses hstore type.
PhpStorm uses postgresql-9.4-1201.jdbc4.jar driver. I don't know if it is driver error or PhpStorm error. I know that in pgAdmin it works and in PhpStorm not.
I work with same environment as Vojtěch and I have found that the extension is indeed created and present. But in different schema (public) then the current connection operates (the PostgreSQL search_path). There is probably bug in PhpStorm as its not respecting PostgreSQL user's default search_path.
Some workarounds (for DB console only):
In database console you use RESET SEARCH_PATH; statement.
You can enforce search_path on JDBC connection, see the question.

Make HSQLDB Compatibility with DB2

HSQL-2.3.2 is using as in-memory to test the application which is integrated DB2.
So I am setting sql.syntax_db2=true in url as below in order to provide DB2 compatibility
jdbc:hsqldb:file:{path};shutdown=true;sql.syntax_db2=true
when above url using "SET DATABASE SQL SYNTAX DB2 FALSE" statement created in generated script file,but when i am passing db2,ora syntax true as below
jdbc:hsqldb:file:{path};shutdown=true;sql.syntax_db2=true;sql.syntax_ora=true
then both statements SET DATABASE SQL SYNTAX DB2 TRUE and SET DATABASE SQL SYNTAX ORA TRUE are created and db2 syntax is not executing and below exception firing
nested exception is java.sql.SQLSyntaxErrorException: unexpected token: IGNORE
So please share where needs to make change in order to make DB2 compatibility?
Thanks,
Durgesh