postgresql 什么是数据类型 bytea,我什么时候使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34486931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:07:55  来源:igfitidea点击:

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

postgresqldefinitionbytea

提问by Walker Farrow

In Postgres there is a datatype called bytea

在 Postgres 中有一种数据类型叫做 bytea

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

Postgres 文档在这里: 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 byteaseveral times and starting to wonder to myself "It seems like they expect me to understand this... Maybe I should find out what it is."

我已经bytea多次遇到这个词,并开始对自己感到疑惑:“似乎他们希望我理解这一点……也许我应该找出它是什么。”

If anybody can give a simple definition for it and some given circumstance of when I would possibly use it, that would be super helpful.

如果有人可以为它给出一个简单的定义以及我何时可能使用它的某些特定情况,那将非常有帮助。

Thanks.

谢谢。

采纳答案by Jim Dennis

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

我认为文档对byteatext之间的区别相当清楚:

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.

二进制字符串与字符串的区别有两个方面。首先,二进制字符串特别允许存储值为零的八位字节和其他“不可打印”八位字节(通常是 32 到 126 范围之外的八位字节)。字符串不允许零八位字节,也不允许根据数据库的选定字符集编码无效的任何其他八位字节值和八位字节值序列。其次,对二进制字符串的操作处理实际字节,而字符串的处理取决于区域设置。简而言之,二进制字符串适合存储程序员认为是“原始字节”的数据,而字符串适合存储文本。

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

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").

...它与内容是否为“文本”(取决于您应用于服务器配置和运行它的操作系统的区域设置和国际化设置)与“八位字节”数组( 8 位二进制值序列——通常称为“字节”)。

(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).

(术语“字节”和术语“八位字节”之间存在一些技术上的区别——因为从历史上看,一些平台和计算设备使用“字节”和奇偶校验和/或停止位,而术语“八位字节”总是意味着正好是 8 -bits;为阐明网络协议的规范和文档而引入的术语)。

回答by indika

VARBINARY, which is an equivalent to BYTEAin Postgres, can be used to store JWTaccess tokens.

VARBINARY,相当于BYTEAin Postgres,可用于存储JWT访问令牌。

create table oauth_access_token (
  token_id VARCHAR(255),
  token BYTEA,
  .......
)