How to create a SET-like type on postgresql

Use the HSTORE column type. HSTORE stores key/value pairs. You can use null values if you only care about checking if a key exists. See https://www.postgresql.org/docs/current/static/hstore.html. For example, to ask Is ‘x’ in my hstore?, do CREATE EXTENSION HSTORE; –create extension only has to be done once SELECT * FROM ‘x=>null,y=>null,z=>null’::HSTORE ? ‘x’; I believe … Read more

Postgres: \copy syntax

About the permissions: Don’t forget that to access a file you need permissions on all directories in the path. So if, for example, the OS user postgres does not have permissions on the /home/MyUser directory, you get the observed error message. About \copy: You have to use the -c option to supply a command to … Read more

How to compare character varying (varcar) to UUID in PostgreSQL?

uuid is a specific datatype. To you it looks like text, but it’s not. You cannot compare uuid using string functions (uuid like “abc%”), or compare it with text. As Tamer suggests, you can cast it first, if you need to compare. SELECT * FROM (SELECT ‘A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11’::uuid as my_uuid) foo WHERE my_uuid::text like ‘a%’ For … Read more

Filtering by window function result in Postgresql

I don’t know if this qualifies as “more elegant” but it is written in a different manner than Cybernate’s solution (although it is essentially the same) WITH window_table AS ( SELECT s.*, sum(volume) OVER previous_rows as total FROM stuff s WINDOW previous_rows as (ORDER BY priority desc ROWS between UNBOUNDED PRECEDING and CURRENT ROW) ) … Read more