Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

396
Views
PNG images in postgresql database

I would like to know how can I insert an image "bytea" into a table of my postgresql database, with the condition that the image must be a png file.

Here is the table:

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

How can I ensure that any files inserted into the table must only be PNG files?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The best you can do is to write a check constraint that extracts the first few bytes from the bytea and checks for the appropriate magic number:

CHECK (substr(image, 1, 8) = BYTEA '\x89504E470D0A1A0A')

Of course there is no guarantee that no other file starts with these bytes, but that is the best you can do short of checking the whole file for consistency.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!