Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get size for Bytea column in postgreSQL

Tags:

postgresql

I have table for store file information. File content is stored in column (column datatype is bytea in PostgreSQL). Now I want to get file content size, based on bytea column data.

In SQL Server, I am able to get by using below query:

select convert(nvarchar(50),DATALENGTH(FileDataInVarbinary)/ 1048576.0 ) +' MB' as FileSize from Table_FileInfo

Could you suggest me, how we can write in postgreSQL for same above sql query.

like image 524
Jagadeesh Avatar asked Feb 13 '20 05:02

Jagadeesh


Video Answer


1 Answers

You can use the length() function to get the length in bytes:

select length(bytea_column) as filesize
from file_info;

If you want to format the output you can use pg_size_pretty()

select pg_size_pretty(length(bytea_column)) as filesize
from file_info;

That will adapt the "kb", "mb" output based on the size. If you always want MB, you can use:

select concat(length(bytea_column) / 1048576.0, ' MB') as filesize
from file_info;
like image 110
a_horse_with_no_name Avatar answered Sep 28 '22 02:09

a_horse_with_no_name