Query bytea field in postgres via command line - postgresql

I have a table with a bytea field, and it would be convenient if I could do queries via the command line (or pgAdmin's query executor). I've got the hex value as a string. Is there a built in function to convert hex to bytea?
I'd like to do something like:
SELECT * FROM table WHERE my_bytea_field=some_???_function('fa26e312');
where 'fa26e312' is the hex value of the bytea field I want.
Note: this is just to be helpful while I'm developing / debugging things, I can do it via code but I'd like to be able to do it by hand in a query.

Try using built-in decode(string text, type text) function (it returns bytea). You can run queries via CLI using psql in non-interactive mode, that is with -c switch (there are some formatting options if you like):
psql -c "SELECT * FROM table WHERE my_bytea_field=decode('fa26e312', 'hex');"
Example:
CREATE TABLE test(id serial, my_bytea_field bytea);
INSERT INTO test (my_bytea_field) VALUES
(E'\\320\\170'::bytea),
(E'\\100\\070'::bytea),
(E'\\377\\377'::bytea);
psql -tc "SELECT * FROM test WHERE my_bytea_field=decode('ffff', 'hex');"
3 | \377\377

SELECT * FROM table WHERE my_bytea_field=E'\\xfa26e312';
Just as in the example in the Binary Data Types docs (note the E'\\x' prefix):
SELECT E'\\xDEADBEEF';

under psql console, you can see its bytea content by default, such as:
also you can query like this (such as in pg_admin) :
xxx=# select id, encode(code_hash, 'hex') from your_table where id = 1195;
id | encode
------+------------------------------------------------------------------
1195 | c5e5dcf215925f7ef4dfaf5f4b4f105bc321c02776d6e7d52a1db3fcd9d011a4

Related

Extract clob postgres value in psql command

With theses row in my table :
From intellij, when I run my PSQL request, lo_get result is correctly return (here a JSON content)
Select lo_get(cast(my_col as bigint)) from my_table
my_column is "text" format
But when i execute the same request in psql console on my postgres server, the result is not correctly return
example :
\x7b22646174654c6976726169736f6e223a22323032322d30322d32325432333a30303a30302e3030302b30303030222c22666f726d617443616e74696e6573223a5b5d2c226d6f6e74616e7450616e696572456e436f757273223a307d
Is there a way to get these value in psql columns ?
If I encode it in 'escape' method, I get something which looks like JSON, but which doesn't look anything at all like what you show in your image (which does not look like JSON in the least).
select encode('\x7b22646174654c6976726169736f6e223a22323032322d30322d32325432333a30303a30302e3030302b30303030222c22666f726d617443616e74696e6573223a5b5d2c226d6f6e74616e7450616e696572456e436f757273223a307d'::bytea,
'escape');
If you want to always encode it like this, you could set bytea_output TO escape. But while that might make sense in the psql interactive tool, it probably isn't preferable as a permanent server setting, i.e. when dealing with other clients.

What is the postgresql equivalent of DUMP function in Oracle?

The dump function in Oracle displays the internal representation of data:
DUMP returns a VARCHAR2 value containing the data type code, length in bytes, and internal representation of expr
Fore example:
SELECT DUMP(cast(1 as number ))
2 FROM DUAL;
DUMP(CAST(1ASNUMBER))
--------------------------------------------------------------------------------
Typ=2 Len=2: 193,2
SQL> SELECT DUMP(cast(1.000001 as number ))
2 FROM DUAL;
DUMP(CAST(1.000001ASNUMBER))
--------------------------------------------------------------------------------
Typ=2 Len=5: 193,2,1,1,2
It shows that the first 1 uses 2 byte for storing and the second example uses 5 bytes for storing.
I suppose the similar function in PostgreSQL is pg_typeof but it returns only the type name without information about byte usage:
SELECT pg_typeof(33);
pg_typeof
integer (1 row)
Does anybody know if there is an equivalent function in PostgreSQL?
I don't speak PostgreSQL.
However, Oracle functionality page says that there's Orafce which
implements in Postgres some of the functions from the Oracle database that are missing (or behaving differently)
It, furthermore, mentions the dump function
dump (anyexpr [, int]): Returns a text value that includes the datatype code, the length in bytes, and the internal representation of the expression
One of examples looks like this:
postgres=# select pg_catalog.dump('Pavel Stehule',10);
dump
-------------------------------------------------------------------------
Typ=25 Len=17: 68,0,0,0,80,97,118,101,108,32,83,116,101,104,117,108,101
(1 row)
To me, it looks like Oracle's dump:
SQL> select dump('Pavel Stehule') result from dual;
RESULT
--------------------------------------------------------------
Typ=96 Len=13: 80,97,118,101,108,32,83,116,101,104,117,108,101
SQL>
I presume you'll have to visit GitHub and install the package to see whether you can use it or not.
It is not a complete equivalent, but if you want to figure out the byte values used to encode a string in PostgreSQL, you can simply cast the value to bytea, which will give you the bytes in hexadecimal:
SELECT CAST ('schön' AS bytea);
This will work for strings, but not for numbers.

Postgres search ASCII against UTF8

I created my database like this:
CREATE DATABASE MYDATABASE WITH ENCODING 'UTF8' LC_COLLATE='ro_RO.utf8' TEMPLATE template0;
And I have a table county populated with UTF8 county names
This query returns the result as I expect:
SELECT * from county WHERE name='Iași'; (note the comma under s, known as S-comma)
However, this query returns no result:
SELECT * from county WHERE name='Iasi'; (note that the comma has disappeared)
How should I create the database such that the second query also works? I'm switching to postgres, but when I was using MySQL, everything worked fine.

Postgres psql output strings without escape characters

I have a column in PostgreSQL that is of type bytea that usually has text data. I want to get the value of that column for a certain row with newlines and tabs intact rather than the octal escape characters that psql is outputting. For example, I run:
psql -Atc 'SELECT my_column from my_table limit 1;'
And I get output like:
Foo\015\012Bar\011This is some text.
Instead I want:
Foo
Bar This is some text.
I realize I can just use grep, which is what I'm doing, but I'm wondering if there's some simple way to do this via psql. I tried type casting the value to type text, but that didn't seem to help.
Try to convert your bytea in this way to ascii:
psql -Atc "SELECT convert_from (my_column, 'SQL_ASCII') from my_table limit 1;"

Postgresql: dblink in Stored Functions from local to remote database

I check below link which I used and running perfectly. But I want to opposite this things.
Postgresql: dblink in Stored Functions
My scenario: Two databases are there. I want to copy one table data from local to remote database. I used dblink for this used but I am confused how to use dblink to store the data?
Local database name: localdatabase
Remote Database name: remotedatabase
Can any one suggest me how can I do this?
Thanks in advance.
Something like the lines below should work:
SELECT dblink_connect('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd');
-- change the connection string to your taste
SELECT dblink_exec('INSERT INTO test (some_text) VALUES (''Text go here'');');
Where test is a table in the remote database with the following definition:
CREATE TABLE test(
id serial
, some_text text
);
After running dblink_exec(), you can check the results in the remote database (or locally, using dblink(), like in the example below).
SELECT * FROM dblink('SELECT id, some_text FROM test') AS d(id integer, some_text text);
id | some_text
----+--------------
1 | Text go here
(1 row)
You can wrap your dblink_exec call in a function as well:
CREATE OR REPLACE FUNCTION f_dblink_test_update(val text, id integer) RETURNS text AS
$body$
SELECT dblink_exec('UPDATE torles.test SET some_text=' || quote_literal($1) || ' WHERE id = ' || $2);
$body$
LANGUAGE sql;
As you can see, you can even build your query string dynamically. (Not that I advocate this approach, since you have to be careful not to introduce a SQL injection vulnerability into your system this way.)
Since dblink_exec returns with a text message about what it did, you have to define your function as RETURNS text unless there are other value-returning statements after the dblink_exec call.