Postgres16 常见问题

input file appears to be a text format dump. Please use psql

https://stackoverflow.com/questions/40632228/input-file-appears-to-be-a-text-format-dump-please-use-psql

How force pg_dump to (not) include scheme name for each objects in DDL

https://stackoverflow.com/questions/58146760/how-force-pg-dump-to-not-include-scheme-name-for-each-objects-in-ddl?utm_source=chatgpt.com

ERROR: could not determine data type of parameter $1

https://stackoverflow.com/questions/56089400/postgres-sql-could-not-determine-data-type-of-parameter-by-hibernate
着重的说一下这个情况。
先说下我这边的环境:
postgres 16版本
springboot 3

出现这个问题一般是由于配置的jdbc连接导致的问题。
首先检查stringtype参数是否配置为stringtype=unspecified了,如果配置的话,请去掉这个再尝试下。

stringtype解释(这在postgresql的JDBC手册中有解释):
指定绑定通过 setString() 设置的 PreparedStatement 参数时使用的类型。如果 stringtype 设置为 VARCHAR(默认),此类参数将作为 varchar 参数发送到服务器。如果 stringtype 被设置为 unspecified,参数将以无类型值的形式发送到服务> 器,服务器将尝试推断出适当的类型。如果现有应用程序使用 setString() 来设置实际上是其他类型(如整数)的参数,并且无法更改应用程序以使用适当的方法(如 setInt()),则此方法非常有用。

如果你因为某些情况不能去掉或者不想去掉的话,那么有几种方式可以让你的SQL正常运行:
第一种就是在SQL语句(这里使用Mybatis的XML文件举例)中的占位符处添加类型转换,
#{name}::varchar这种的,当然cast函数进行类型转换也可以,他们是等同的作用,
举例:
``

select * from user where username = #{username}::varchar;

这个SQL运行着就没有问题,因为加上了::varchar类型转换。

上面SQL这个情况即使不加类型转换,也不会报错,因为PG数据库会隐式的转换。

第二种就是使用了函数,函数里又使用了占位符。
如:

select * from user where username = concat('%', #{username}, '%');

这个sql运行后就会出现上述的错误信息。因为#{}里的值会在PG解析式修改为?, 而?占位符处的值类型在绑定占位符值的期间postgre数据库不知道(因为在jdbc连接处定义了stringtype=unspecified参数)。
需要改成以下这种方式才能运行:

select * from user where username = concat('%', #{username}::varchar, '%');
select * from user where username = '%' || #{username} || '%';

引用

https://jdbc.postgresql.org/
https://postgresql.org/

posted @ 2025-07-18 16:53  星小梦  阅读(72)  评论(0)    收藏  举报