I’ve created a sql dump file using pg_dump. This export file contains functions which contain $$ characters. No problem to import the file with psql -f < filename>.
If want to import the file with ant using the SQLExec task, I get an exception like:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"
Is there a way to import a file containing $$?
In the postgres Log it seems that the SQLExec tasks converts $$ to $ which the causes the error.
ERROR: syntax error at or near “$” at character 87
STATEMENT: CREATE FUNCTION process_create_servicenumber() RETURNS trigger LANGUAGE plpgsql AS $ BEGIN IF (TG_OP = ‘DELETE’) THEN RETURN OLD
Here my method
protected void importNewDbFromDumpFile() {
final class SqlExecuter extends SQLExec {
public SqlExecuter() {
Project project = new Project();
project.init();
setProject(project);
setTaskType("sql");
setTaskName("sql");
}
}
try {
SqlExecuter executer = new SqlExecuter();
executer.setSrc(new File(dbDumpFileLocation));
executer.setClasspath(createClasspath());
executer.setEscapeProcessing(true);
executer.setDriver("org.postgresql.Driver");
executer.setUrl("jdbc:postgresql://localhost/test");
executer.setPassword("test");
executer.setUserid("manager");
executer.execute();
} catch (Exception e) {
log.info("Exception importing database ...", e);
}
}
$$is just the bare minimum for dollar-quoting. Make it (much!) less likely to conflict with strings in the enclosed literal by putting a string between the dollars:More advice
The assignment operator in plpgsql is
:=.=is undocumented and may go away in future releases. More under this related question.Use
CURRENT_DATEinstead ofCURRENT_TIMESTAMP::date.It is allowed, but I would advise not to use mixed case parameter names in plpgsql. They are case insensitive.
Most importantly, simplify:
Or even:
Can be declared
STABLE!age()in PostgreSQL doing almost, but not quite, the same: it returns a “symbolic” result with standard-years and months. Therefore, expression withage()can yield different results for longer periods of time.These are all equivalent – except for the last two deviating with longer periods of time:
As to the original question: this PostgreSQL error message does not necessarily mean the problem is with the dollar sign:
Most of the time it’s a missing
;before that line. Or maybe an un-escaped special characters in XML, like< > &? The dollar sign$should be fine. But I am no expert with ant. There should be more context in the PostgreSQL log.