I need to fix few lines in Jira configuration in PostgreSQL. Unfortunatelly it uses dot notation in columns and I cannot fine a way how to escape it.
select * from "cwd_directory_attribute";
directory_id | attribute_name | attribute_value
1 | user_encryption_method | atlassian-security
10000 | ldap.user.filter | (objectclass=person)
No escaping:
psql:fix_directory.sql:1: ERROR: column "ldap" of relation "cwd_directory_attribute" does not exist
LINE 1: update cwd_directory_attribute set ldap.basedn='ou=sandbox,d...
Single quote:
psql:fix_directory.sql:1: ERROR: syntax error at or near "'ldap.basedn'"
LINE 1: update cwd_directory_attribute set 'ldap.basedn'='ou=sandbox...
Double quote:
psql:fix_directory.sql:1: ERROR: column "ldap.basedn" of relation "cwd_directory_attribute" does not exist
LINE 1: update cwd_directory_attribute set "ldap.basedn" ='ou=sandb...
Square bracket:
psql:fix_directory.sql:1: ERROR: syntax error at or near "["
LINE 1: update cwd_directory_attribute set [ldap.basedn] ='ou=sandb...
E.g.
update cwd_directory_attribute set "ldap.basedn" ='ou=sandbox,dc=eu' where directory_id=10100;
I found answers that double quotes shall work but they do not. I could modify the columns manually in some SQL editor, but I want to set up a script:
psql -f fix_directory.sql jiradb7
What is the proper escaping for columns in UPDATE procedure?
Update:
select "ldap.secure" from "cwd_directory_attribute";
ERROR: syntax error at or near "select"
LINE 2: select "ldap.secure" from "cwd_directory_attribute";
You are confusing column values and column names.
Your table has a column named attribute_name which contains the value 'ldap.user.filter' (and supposedly 'ldap.basedn' in a different row).
So what you want is:
update cwd_directory_attribute
set attribute_value = 'ou=sandbox,dc=eu'
where directory_id = 10100
and attribute_name = 'ldap.basedn';
Related
Consider the following xml document that is stored in a PostgreSQL field:
<E_sProcedure xmlns="http://www.minushabens.com/2008/FMSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" modelCodeScheme="Emo_ex" modelCodeSchemeVersion="01" modelCodeValue="EMO_E_PROCEDURA" modelCodeMeaning="Section" sectionID="11">
<tCatSnVsn_Pmax modelCodeScheme="Emodinamica_referto" modelCodeSchemeVersion="01" modelCodeValue="tCat4" modelCodeMeaning="My text"><![CDATA[1]]></tCatSnVsn_Pmax>
</E_sProcedure>
If I run the following query I get the correct result for Line 1, while Line 2 returns nothing:
SELECT
--Line 1
TRIM(BOTH FROM array_to_string((xpath('//child::*[#modelCodeValue="tCat4"]/text()', t.xml_element)),'')) as tCatSnVsn_Pmax_MEANING
--Line2
,TRIM(BOTH FROM array_to_string((xpath('/tCatSnVsn_Pmax/text()', t.xml_element)),'')) as tCatSnVsn_Pmax
FROM (
SELECT unnest(xpath('//x:E_sProcedure', s.XMLDATA::xml, ARRAY[ARRAY['x', 'http://www.minushabens.com/2008/FMSchema']])) AS xml_element
FROM sr_data as s)t;
What's wrong in the xpath of Line 2?
Your second xpath() doesn't return anything because of two problems. First: you need to use //tCatSnVsn_Pmax as the xml_element still starts with <E_sProcedure>. The path /tCatSnVsn_Pmax tries to select a top-level element with that name.
But even then, the second one won't return anything because of the namespace. You need to pass the same namespace definition to the xpath(), so you need something like this:
SELECT (xpath('/x:tCatSnVsn_Pmax/text()', t.xml_element, ARRAY[ARRAY['x', 'http://www.minushabens.com/2008/FMSchema']]))[1] as tCatSnVsn_Pmax
FROM (
SELECT unnest(xpath('//x:E_sProcedure', s.XMLDATA::xml, ARRAY[ARRAY['x', 'http://www.minushabens.com/2008/FMSchema']])) AS xml_element
FROM sr_data as s
)t;
With modern Postgres versions (>= 10) I prefer using xmltable() for anything nontrivial. It makes passing namespaces easier and accessing multiple attributes or elements.
SELECT xt.*
FROM sr_data
cross join
xmltable(xmlnamespaces ('http://www.minushabens.com/2008/FMSchema' as x),
'/x:E_sProcedure'
passing (xmldata::xml)
columns
sectionid text path '#sectionID',
pmax text path 'x:tCatSnVsn_Pmax',
model_code_value text path 'x:tCatSnVsn_Pmax/#modelCodeValue') as xt
For your sample XML, the above returns:
sectionid | pmax | model_code_value
----------+------+-----------------
11 | 1 | tCat4
When I am removing a key from a HSTORE, I get the error 'Unexpected end of string':
DB=# UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
ERROR: Unexpected end of string
LINE 1: UPDATE mytable SET properties = properties - 'key' where ...
When I explicitly cast that string, it does work:
DB=# UPDATE mytable SET properties = properties - 'key'::text where label = '9912345678';
UPDATE 1
Why does it give this error message? Isn't 'key' a TEXT column? Or at least a string with a expected end?
The operator is overloaded so the right-hand operand should be explicitly cast. Note the example usage for text, text[] and hstore in the documentation:
'a=>1, b=>2, c=>3'::hstore - 'b'::text
'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']
'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore
In the statement
UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
the string 'key' may be resolved as text or hstore. The first choice of the hstore algorithm is hstore, so the statement is resolved to
UPDATE mytable SET properties = properties - 'key'::hstore where label = '9912345678';
and raises the error
ERROR: Unexpected end of string
though the error message could be more informative, like Unexpected end of string while parsing hstore constant.
I want to insert a query string into a Postgres database column in the following format
{"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}
I try this:
UPDATE reports SET raw_query = {"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''} WHERE id=37;
It gives error like
ERROR: syntax error at or near "{"
LINE 1: UPDATE base_reports SET extra_query = {"enrolled_time":'''SE...
When I try using single quotes it throws error like following:
ERROR: syntax error at or near "SELECT"
LINE 1: ...DATE reports SET raw_query = '{"enrolled_time":'''SELECT DIS...
How can I overcome this situation
Use dollar quoting:
UPDATE reports
SET raw_query = $${"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}$$
WHERE id = 37;
using PGAdminqueries:
SELECT * FROM analyzed_users;
SELECT * FROM time_table;
runs successfully. But query:
SELECT * FROM analyzed_users, time_table WHERE analyzed_users.id = time_table.userId
returns error:
ERROR: column analyzed_users.id does not exist
LINE 2: SELECT * FROM analyzed_users, time_table WHERE analyzed_user...
********** Error **********
ERROR: column analyzed_users.id does not exist
SQL state: 42703
Character: 49
I'm struggling with it for a while and I have no idea why it doesn't want to work..
The problem is in the WHERE clause in the second query:
WHERE analyzed_users.id = time_table.userId
The error is saying that analyzed_users.id doesn't exist in that table.
Check for and use the actual name of the column in analyzed_users that you want to compare to time_table.userId in the second query. That should fix the problem.
I would like to import a csv file, which has multiple occurrences of missing values. I recoded them into NULL and tried to import the file as. I suppose that my attributes which include the NULLS are character values. However transforming them to numeric is bit complicated. Therefore I would like to import all of my table as:
\copy player_allstar FROM '/Users/Desktop/Rdaten/Data/player_allstar.csv' DELIMITER ';' CSV WITH NULL AS 'NULL' ';' HEADER
There must be a syntax error. But I tried different combinations and always get:
ERROR: syntax error at or near "WITH NULL"
LINE 1: COPY player_allstar FROM STDIN DELIMITER ';' CSV WITH NULL ...
I also tried:
\copy player_allstar FROM '/Users/Desktop/Rdaten/Data/player_allstar.csv' WITH(FORMAT CSV, DELIMITER ';', NULL 'NULL', HEADER);
and get:
ERROR: invalid input syntax for integer: "NULL"
CONTEXT: COPY player_allstar, line 2, column dreb: "NULL"
I suppose it is caused by preprocessing with R. The Table came with NAs so I change them to:
data[data==NA] <- "NULL"
I`m not aware of a different way chaning to NULL. I think this causes strings. Is there a different way to preprocess and keep the NAs(as NULLS in postgres of course)?
Sample:
pts dreb oreb reb asts stl
11 NULL NULL 8 3 NULL
4 5 3 8 2 1
3 NULL NULL 1 1 NULL
data type is integer
Given /tmp/sample.csv:
pts;dreb;oreb;reb;asts;stl
11;NULL;NULL;8;3;NULL
4;5;3;8;2;1
3;NULL;NULL;1;1;NULL
then with a table like:
CREATE TABLE player_allstar (pts integer, dreb integer, oreb integer, reb integer, asts integer, stl integer);
it works for me:
\copy player_allstar FROM '/tmp/sample.csv' WITH (FORMAT CSV, DELIMITER ';', NULL 'NULL', HEADER);
Your syntax is fine, the problem seem to be in the formatting of your data. Using your syntax I was able to load data with NULLs successfully:
mydb=# create table test(a int, b text);
CREATE TABLE
mydb=# \copy test from stdin WITH(FORMAT CSV, DELIMITER ';', NULL 'NULL', HEADER);
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> col a header;col b header
>> 1;one
>> NULL;NULL
>> 3;NULL
>> NULL;four
>> \.
mydb=# select * from test;
a | b
---+------
1 | one
|
3 |
| four
(4 rows)
mydb=# select * from test where a is null;
a | b
---+------
|
| four
(2 rows)
In your case you can substitute to NULL 'NA' in the copy command, if the original value is 'NA'.
You should make sure that there's no spaces around your data values. For example, if your NULL is represented as NA in your data and fields are delimited with semicolon:
1;NA <-- good
1 ; NA <-- bad
1<tab>NA <-- bad
etc.