postgresql数据库中的PNG图像

postgresql数据库中的PNG图像,sql,postgresql,Sql,Postgresql,我想知道,在图像必须是png文件的情况下,如何将图像“bytea”插入到postgresql数据库的表中 这是表格: "id_category" SERIAL, "category_name" TEXT, "category_image" bytea, constraint id_cat_pkey primary key ("id_category"))without oids; 如何确保插入到表中的任何文件只能是

我想知道,在图像必须是png文件的情况下,如何将图像“bytea”插入到postgresql数据库的表中

这是表格:

"id_category" SERIAL,
"category_name" TEXT,
"category_image" bytea,
constraint id_cat_pkey primary key ("id_category"))without oids;

如何确保插入到表中的任何文件只能是PNG文件?

您最好编写一个检查约束,从
bytea
中提取前几个字节,并检查适当的:


当然,不能保证没有其他文件以这些字节开头,但除了检查整个文件的一致性之外,这是最好的方法。

如何做到这一点(特别是提取必要的字节)?谢谢-正是我所需要的!
CHECK (substr(image, 1, 8) = BYTEA '\x89504E470D0A1A0A')