postgresql-9.4

Postgresql - Regex split csv line with potentials quotes

放肆的年华 提交于 2019-12-02 00:43:14
I would like to split a column that represent a csv line in postgres. Fields in this text line are delimited by pipe, sometime they are enclosed by quote and sometime not. In addition we can have escaped chars. field1|"field2"|field3|"22 \" lcd \| screen " Is there a regex to split this column (i.e. using regexp_split_to_array(....)? ) Abelisto Not about regexp but it works create or replace function split_csv( line text, delim_char char(1) = ',', quote_char char(1) = '"') returns setof text[] immutable language plpythonu as $$ import csv return csv.reader(line.splitlines(), quotechar=quote

Postgres function returning a row as JSON value

家住魔仙堡 提交于 2019-12-01 21:54:11
I am pretty new to PG, and trying to convert from MSSQL. I am working on a function that will return JSON results. This one works: Create Or Replace Function ExampleTable_SelectList() Returns JSON As $$ Select array_to_json(array_agg(row_to_json(t))) From (Select id, value1, value2, From ExampleTable) t $$ Language SQL; Now, I want to call can update that returns a value and turn that value into JSON to return. So, this one gives an error on the set command. Create Or Replace Function ExampleTable_Update (id bigint, value1 text) Returns JSON As $$ Select row_to_json(t) From ( Update

Use variable set by psql meta-command inside of DO block

大兔子大兔子 提交于 2019-12-01 21:35:39
Here's what I would like to do: \set values foo,bar,baz DO $$ DECLARE value TEXT; values TEXT[] := string_to_array(:'values', ','); BEGIN FOREACH value IN ARRAY values LOOP raise notice 'v: %', value; END LOOP; END $$ LANGUAGE plpgsql; Which results in the following error: ERROR: syntax error at or near ":" SELECT string_to_array(:'values', ',') INTO values... ^ Here's the solution I have currently, but it feels hacky: \set values foo,bar,baz PREPARE get_values AS SELECT string_to_array(:'values', ','); DO $$ DECLARE value TEXT; values TEXT[]; BEGIN EXECUTE 'EXECUTE get_values' INTO values;

Error trying to run pgAdmin4

大城市里の小女人 提交于 2019-12-01 15:23:39
I've installed postgresql 9.6 (using suggested linux installer) which comes with pgAdmin4, but getting a few errors. First, I had to change the folder name from "pgAdmin 4" (note the extra space) to "pgAdmin4" to avoid "file not found error". Then I run sudo python pgAdmin4.py and got the following error: Traceback (most recent call last): File "../../pgAdmin4.py", line 24, in <module> from pgadmin import create_app File "/opt/PostgreSQL/9.6/pgAdmin4/web/pgadmin/__init__.py", line 17, in <module> from flask import Flask, abort, request, current_app ImportError: No module named flask I tried

Error trying to run pgAdmin4

一笑奈何 提交于 2019-12-01 14:24:45
问题 I've installed postgresql 9.6 (using suggested linux installer) which comes with pgAdmin4, but getting a few errors. First, I had to change the folder name from "pgAdmin 4" (note the extra space) to "pgAdmin4" to avoid "file not found error". Then I run sudo python pgAdmin4.py and got the following error: Traceback (most recent call last): File "../../pgAdmin4.py", line 24, in <module> from pgadmin import create_app File "/opt/PostgreSQL/9.6/pgAdmin4/web/pgadmin/__init__.py", line 17, in

How to get distinct array elements with postgres?

送分小仙女□ 提交于 2019-12-01 07:21:27
问题 I have an array with duplicate values in postgres. For example: SELECT cardinality(string_to_array('1,2,3,4,4', ',')::int[]) as foo => "foo"=>"5" I would like to get unique elements, for example: SELECT cardinality(uniq(string_to_array('1,2,3,4,4', ',')::int[])) as foo => -- No function matches the given name and argument types. You might need to add explicit type casts. Can I get unique elements of an array in postgres without using UNNEST ? 回答1: For integer arrays use intarray extension:

how to use jsonb in rails

僤鯓⒐⒋嵵緔 提交于 2019-12-01 00:30:59
I have a rails project with Postgresql 9.4 as backend. I have column like this: t.json :slot_details, null: false, default: {} How do I change this to JSONB from JSON? Should I add index and that will be changed to JSONB? For migrating this, you can do the following. Payload in this case was originally a json field. class AlterJsonbToJsonAndBack < ActiveRecord::Migration def up change_column :dynamics, :payload, 'jsonb USING CAST(payload AS jsonb)' end def down change_column :dynamics, :payload, 'json USING CAST(payload AS json)' end end To find out how to query jsonb in Rails 4.2, checkout

Wildfly 10 Final postgres driver ClassCastException

和自甴很熟 提交于 2019-11-30 16:08:13
问题 eventually somebody can help me. Currently I have a really strange problem when starting wildfly 10 Final with a postgres driver but with the same setting wildfly 10 CR4 will start up. The exception I get is following: Caused by: javax.resource.ResourceException: IJ031089: Failed to load datasource: org.postgresql.Driver at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getDataSource(LocalManagedConnectionFactory.java:650) at org.jboss.jca.adapters.jdbc.local

Wildfly 10 Final postgres driver ClassCastException

和自甴很熟 提交于 2019-11-30 15:39:31
eventually somebody can help me. Currently I have a really strange problem when starting wildfly 10 Final with a postgres driver but with the same setting wildfly 10 CR4 will start up. The exception I get is following: Caused by: javax.resource.ResourceException: IJ031089: Failed to load datasource: org.postgresql.Driver at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getDataSource(LocalManagedConnectionFactory.java:650) at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:311) ... 6 more Caused by

LATERAL JOIN not using trigram index

ぃ、小莉子 提交于 2019-11-30 04:05:59
问题 I want to do some basic geocoding of addresses using Postgres. I have an address table that has around 1 million raw address strings: => \d addresses Table "public.addresses" Column | Type | Modifiers ---------+------+----------- address | text | I also have a table of location data: => \d locations Table "public.locations" Column | Type | Modifiers ------------+------+----------- id | text | country | text | postalcode | text | latitude | text | longitude | text | Most of the address strings