Postgres escape символы

Обновлено: 02.07.2024

1. How to input special characters in a string, such as carriage return.

2. How to enter single quotation marks in a string.

3. What is an escape character? Usage.

4. Is backslash an escape character in the SQL standard? How does PostgreSQL use backslash escape? How about the writing format?

5. Why is there a risk of SQL injection when escaping single quotes? What parameters of the database can control this risk? What parameters can be warned?

6. How to input strings in the form of UNICODE.

7. Enter in UNICODE format, support all character sets? What character sets are currently supported and what CODE range are supported?

8. What's wrong with entering UNICODE value in Greenplum's JSON? Is there the same problem in PostgreSQL?

1 Answer 1

On PostgreSQL 9.3 or newer and haven't manually changed the standard_conforming_strings setting, you don't have to escape anything except single quotes, which you double like this:

If you're on an older version, set standard_conforming_strings to on in postgresql.conf , or double backslashes and use the E'' string notation:

However, you should really be using your language's PostgreSQL client driver to send parameterized statements that take care of escaping automatically. Failure to do so can lead to SQL injection holes - see this site.

попытка вставить escape-символ в таблицу приводит к появлению предупреждения.

( использование PSQL 8.2)

кто-нибудь знает как обойти это?

частично. Текст вставляется, но предупреждение все равно генерируется.

Я нашел обсуждение, которое указывало, что текст должен предшествовать "E", как таковой:

это подавило предупреждение, но текст все еще не был возвращен правильно. Когда я добавил дополнительную косую черту, как предложил Майкл, это сработало.

такие как:

Я нахожу крайне маловероятным, что Postgres усекает ваши данные на входе-он либо отклоняет их, либо сохраняет их как есть.

действительно глупый вопрос: вы уверены, что строка усекается, а не просто разбивается на линии, которую вы указываете (и, возможно, не отображается в вашем интерфейсе)? Т. е., вы ожидаете, что поле будет отображаться как

Это будет вставлено \n это не будет будь

Это будет вставлено

этого не будет

кроме того, какой интерфейс вы используете? Можно ли что-то по пути ест ваши обратные косые черты?

Summary

1. How to input special characters in a string, such as carriage return.

2. How to enter single quotation marks in a string.

3. What is an escape character? How does PostgreSQL use backslash escape? How about the writing format?

4. Is backslash an escape character in the SQL standard?

5. Why is there a risk of SQL injection when escaping single quotes? What parameters of the database can control this risk? What parameters can be warned?

6. How to input strings in the form of UNICODE.

7. Enter in UNICODE format, support all character sets? What character sets are currently supported and what CODE range are supported?

8. What's wrong with entering UNICODE value in Greenplum's JSON? Is there the same problem in PostgreSQL?

Attempting to insert an escape character into a table results in a warning.

Produces the warning:

Anyone know how to get around this?

23.4k 6 6 gold badges 60 60 silver badges 69 69 bronze badges 6,713 8 8 gold badges 28 28 silver badges 37 37 bronze badges

unicode input

How do I enter unicode strings?

Users can input UNICODE encoding directly to the database, eliminating the encoding conversion process, but users must ensure that the encoding is consistent with the database server encoding, otherwise there may be coding overflow or scrambling problems. Once saved, it is the same as storing strings directly.

unicode input format 1

When the backslash is configured for escaped characters, enter UNICODE like this

(Note that this input format requires that the backslash must be an escape character, refer to the previous section, if the backslash becomes an escape character)

At present, only UTF8 character sets allow input of unicode greater than 007f, while other character sets can only input unicode in ascii range.

unicode input format 2

Format 2, on the contrary, backslashes are not unicode when escaping characters.

This format supports 2 or 3 bytes of UNICODE.

The format is as follows.

If you want to replace escape characters, use UESCAPE grammar.

Example 2, you can't use escape format

Update 2

This is the code I am executing to get the result set from PostgreSQL:

269k 59 59 gold badges 604 604 silver badges 697 697 bronze badges


1,318 3 3 gold badges 23 23 silver badges 39 39 bronze badges

C Language Style String

Backslash Escape Sequence Interpretation
\b backspace
\f form feed
\n newline
\r carriage return
\t tab
\o, \oo, \ooo (o = 0 - 7) octal byte value
\xh, \xhh (h = 0 - 9, A - F) hexadecimal byte value
\uxxxx, \Uxxxxxxxx (x = 0 - 9, A - F) 16 or 32-bit hexadecimal Unicode character value
\\ Backslash
' Single quotation mark

String Writing

1. Single quotation marks

2. Dollar symbol

Update

this is the error I am getting

Escape configuration

Three configuration controls are escaped as follows:

1. (Related to SQL injection) Is backslash escape single quotation marks allowed?

backslash_quote = on allows, off does not allow, safe_encoding (only when client_encoding does not allow backslash\ to appear in multi-byte characters (most character sets are single-byte representations), then escaped single quotes are allowed.)

Why should we control the escape single quotation marks?

Because this may introduce the risk of SQL injection, such as when the end customer puts at the end, the single quotation mark can be escaped, and the string terminator that should have been typed normally does not exist.

Note that the configuration of backslash_quote only works when standard_conforming_strings=off or when E''is used.

Otherwise, when standard_conforming_strings=on, will be treated as a normal string.

Example 1: When standard_conforming_strings=on, is treated as a normal string

Example 2: The configuration of backslash_quote works when standard_conforming_strings=off or using the E''notation.

2. Whether to output a warning when standard_conforming_strings=off and include backslashes in the''string.

Because in the SQL standard, backslashes in strings are not escape characters, but ordinary characters.

If you want to escape, use the E'.

3. Tell the database whether the backslash in the''string is a common character.

standard_conforming_strings = on backslashes are used as common characters (standard SQL usage), and off backslashes are used as escape characters.

If standard_conforming_strings=on, what about using escape? Sampling E''is enough

How to Transliterate

1. standard_conforming_strings=on(SQL standard), then the backslash in the string''is a common character. Using the E''notation, the backslash is the escape character.

2. Standard_conforming_strings = off (non-SQL standard), then the backslash in the string''is an escape character.

5 Answers 5

Partially. The text is inserted, but the warning is still generated.

I found a discussion that indicated the text needed to be preceded with 'E', as such:

This suppressed the warning, but the text was still not being returned correctly. When I added the additional slash as Michael suggested, it worked.

I am new to postgres, how can I escape the RTF syntax in the MESSAGE parameter of the where clause?

unicode Problem in greenplum JSON

When a unicode native string is stored in the json of greenplum, an error may be reported by using - > extraction. (Estimates are exceptions caused by the conversion process)

Create test tables

Currently it is an escape mode, that is to say, UNICODE will be converted and stored in advance.

Insertion, UNICODE conversion to final string is inserted, extraction is normal

Use non-escape mode, that is to say, UNICODE strings are inserted as they are

At this point, an exception occurs to the extraction.

Using escape mode, UNICODE is escaped and stored in JSON.

There will be no such problem in normal strings, just the JSON type of Greenplum.

The JSON of PG 9.4 has no such problem, as follows.

Читайте также: