Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the datatype bytea and when would I use it?

In Postgres there is a datatype called bytea

The Postgres docs are here for it: http://www.postgresql.org/docs/9.0/static/datatype-binary.html

I cannot understand when I would ever use this - nor can I really understand the purpose of this datatype.

I have come across this term bytea several times and starting to wonder to myself "It seems like they expect me to understand this... Maybe I should find out what it is."

What is a simple definition for it and some circumstances of when I would possibly use it?

like image 233
Walker Farrow Avatar asked Dec 28 '15 02:12

Walker Farrow


People also ask

What is Bytea datatype?

The bytea data type allows the storage of binary strings or what is typically thought of as “raw bytes”. Materialize supports both the typical formats for input and output: the hex format and the historical PostgreSQL escape format. The hex format is preferred.

What is Bytea in Java?

The BYTEA data type stores a binary string in a sequence of bytes. Digital images and sound files are often stored as binary data. EDB Postgres Advanced Server can store and retrieve binary data by way of the BYTEA data type.

What is Bytea SQL Server?

Syntax. BYTEA. Data. Variable-length binary data. Range.

What is the data type for binary data?

Store raw-byte data, such as IP addresses, up to 65000 bytes. Data types BINARY and BINARY VARYING ( VARBINARY ) are collectively referred to as binary string types and the values of binary string types are referred to as binary strings. A binary string is a sequence of octets or bytes.


2 Answers

I think the documentation is reasonably clear on the differences between bytea and text:

Binary strings are distinguished from character strings in two ways. First, binary strings specifically allow storing octets of value zero and other "non-printable" octets (usually, octets outside the range 32 to 126). Character strings disallow zero octets, and also disallow any other octet values and sequences of octet values that are invalid according to the database's selected character set encoding. Second, operations on binary strings process the actual bytes, whereas the processing of character strings depends on locale settings. In short, binary strings are appropriate for storing data that the programmer thinks of as "raw bytes", whereas character strings are appropriate for storing text.

http://www.postgresql.org/docs/9.0/static/datatype-binary.html

... it has to do with whether the contents are "text" (subject to the locale and internationalizations settings you've applied to your server configuration and the OS on which you're running it) vs. arrays of "octets" (sequences of 8-bit binary values --- commonly referred to as "bytes").

(There are some technical distinctions between the term "byte" and the term "octet" -- because, historically, some platforms and computing devices used "bytes" with parity and/or stop bits while the term "octets" always means exactly 8-bits; a term that was introduced to clarify specifications and documentation for networking protocols).

like image 158
Jim Dennis Avatar answered Sep 25 '22 18:09

Jim Dennis


VARBINARY, which is an equivalent to BYTEA in Postgres, can be used to store JWT access tokens.

create table oauth_access_token (   token_id VARCHAR(255),   token BYTEA,   ....... ) 
like image 31
indika Avatar answered Sep 24 '22 18:09

indika