postgresql-9.2

What is the equivalent of regexp \Q…\E in postgresql?

一个人想着一个人 提交于 2019-12-11 02:35:14
问题 I have the following query: SELECT field FROM myTable WHERE field ~ '\\Qprefix[\\E.+' It won't find values like prefix[foo] . How can I replace \Q..\E ? 回答1: This form of regex with a \Q..\E unquoted substring is only supported by PCRE, which is not available natively in PostgreSQL. If your program must deal with this syntax in general, the PCRE support can be installed as an extension, as provided here: https://github.com/petere/pgpcre On the other hand, if it's only that one regex that

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea

不问归期 提交于 2019-12-11 01:57:02
问题 I have been looking everywhere (google, stackoverflow, etc.) for some documentation on how to use the PostgreSQL pg_read_binary_file() function. The only meaningful thing I can find is this page in the official documentation. Every time I try to use this function I get an error. For example: SELECT pg_read_binary_file('/some/path/and/file.gif'); ERROR: absolute path not allowed or SELECT pg_read_binary_file('file.gif'); ERROR: could not stat file "file.gif": No such file or directory Do I

Querying row counts segregated by date ranges

a 夏天 提交于 2019-12-10 23:58:07
问题 I've got a PostgreSQL 9.2.1 database, where I'm attempting and failing to compose a SQL query which will show me the count of distinct tests ( testname ) which failed ( current_status='FAILED' and showing 0 if there were no failures), segregated by month ( last_update ). Here's the table definition: Table "public.tests" Column | Type | Modifiers ----------------+-----------------------------+------------------------------------------------------------- id | bigint | not null default nextval(

Easy way to have return type be SETOF table plus additional fields?

不羁的心 提交于 2019-12-10 22:32:00
问题 I'm writing a PL/pgSQL stored procedure that will return a set of records; each record contains all the fields of an existing table (call it Retailer, which has two fields: retailer_key and retailer_name). This, of course, works: CREATE FUNCTION proc_Find_retailers (IN p_Store_key INT) RETURNS SETOF Retailer AS $$ ...` Now I want to update the sp so that it returns an additional two fields to the 'end' of each returned record. I can do something such as: CREATE FUNCTION proc_Find_store (IN p

Postgresql 9.x: Index to optimize `xpath_exists` (XMLEXISTS) queries

孤人 提交于 2019-12-10 19:19:43
问题 We have queries of the form select sum(acol) where xpath_exists('/Root/KeyValue[Key="val"]/Value//text()', xmlcol) What index can be built to speed up the where clause ? A btree index created using create index idx_01 using btree(xpath_exists('/Root/KeyValue[Key="val"]/Value//text()', xmlcol)) does not seem to be used at all. EDIT Setting enable_seqscan to off , the query using xpath_exists is much faster (one order of magnitude) and clearly shows using the corresponding index (the btree

Can't store array in json field in postgresql (rails) can't cast Array to json

你。 提交于 2019-12-10 15:39:19
问题 This is the error I'm getting when I run db:migrate rake aborted! can't cast Array to json This is my table class CreateTrips < ActiveRecord::Migration def change create_table :trips do |t| t.json :flights t.timestamps end end end This is in my seeds.rb file flights = [{ depart_time_hour: 600, arrive_time_hour: 700, passengers: [ { user_id: 1, request: true } ] }] trip = Trip.create( { name: 'Flight', flights: flights.to_json } ) For some reason I can't do this. If I do this. trip = Trip

Using postgresql xml data type with sqlalchemy

天涯浪子 提交于 2019-12-10 14:38:38
问题 SqlAlchemy supports most database specific data types via dialects, but I could not find anything to work with the postgresql xml column type. Does somebody know a working solution. Idealy it should not require a custom column type implementation by myself. 回答1: If you need to have native 'xml' data type in postgresql database, you need to write custom type which inherited from UserDefinedType not from TypeDecorator. Documentation Here is what I used in one of the projects: import xml.etree

psql - how to flush database content without dropping table

纵然是瞬间 提交于 2019-12-10 13:46:37
问题 I have a table in my db called 'mytable'. I'd like to clear it so that I can continue to collect and analyze 'fresh data' from it. Something like conn = psycopg2.connect(database = mydb_name, host = mydb_server, user = mydb_uname, password = mydb_pwd) cur = conn.cursor() cur.execute("DROP TABLE mytable;") Isn't going to work for me, because as far as I understand it, this destroys the table. I don't want to destroy/re-create... Just to flush all data. How can I work this out? 回答1: Truncate

Retrieve the nextval from a sequence using activerecord in Ruby on Rails 3.2.14 / Ruby 2.0.0 / PostgreSQL 9.2.4

时光毁灭记忆、已成空白 提交于 2019-12-10 01:44:10
问题 This should be SO simple. I want to retrieve the nextval of a sequence... it's not a default value... it's not a primary key... it's not a foreign key. In rare cases, I need a sequence number for a user supplied value. I've tried the following: @nextid = ActiveRecord::Base.connection.execute("SELECT nextval('xscrpt_id_seq')") and what I get back is: #<PG::Result:0x007fe668a854e8 @connection=#<PG::Connection:0x00000003aeff30>> And by using @nextid[0]["nextval"] I can get the correct value, but

Create index on json field in PostgreSQL 9.2

社会主义新天地 提交于 2019-12-09 18:58:52
问题 I have a few tables with json columns and would like to build an index on these columns. However, I get a default operator class (http://www.postgresql.org/docs/9.2/static/sql-createopclass.html). Has someone already done that or give me an alternative? To reproduce the problem, try: >> create table foo (json json, id int); CREATE TABLE >> create index on foo (id); CREATE INDEX >> create index on foo (json); ERROR: data type json has no default operator class for access method "btree" HINT: