postgresql-9.1

PostgreSQL use value from previous row if missing

江枫思渺然 提交于 2019-12-30 10:09:09
问题 I have a following query: WITH t as ( SELECT date_trunc('hour', time_series) as trunc FROM generate_series('2013-02-27 22:00'::timestamp, '2013-02-28 2:00', '1 hour') as time_series GROUP BY trunc ORDER BY trunc ) SELECT DISTINCT ON(trunc) trunc, id FROM t LEFT JOIN ( SELECT id, created, date_trunc('hour', created) as trunc_u FROM event ORDER BY created DESC ) u ON trunc = trunc_u which yields the following result: "2013-02-27 22:00:00"; "2013-02-27 23:00:00";2 "2013-02-28 00:00:00";5 "2013

JPA Hibernate Call Postgres Function Void Return MappingException:

落爺英雄遲暮 提交于 2019-12-30 04:07:07
问题 I have a problem where I am getting an: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111 when trying to call a postgres function using JPA create native query. I created an EJB timer in a startup singleton to run a Postgres function every 6 hours. The function returns void and checks for expired records, deletes them, and updates some statuses. It takes no arguments and it returns void. The postgres function runs perfectly if I call it using PgAdmin query tool (select

JPA Hibernate Call Postgres Function Void Return MappingException:

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 04:07:05
问题 I have a problem where I am getting an: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111 when trying to call a postgres function using JPA create native query. I created an EJB timer in a startup singleton to run a Postgres function every 6 hours. The function returns void and checks for expired records, deletes them, and updates some statuses. It takes no arguments and it returns void. The postgres function runs perfectly if I call it using PgAdmin query tool (select

No password prompt for postgresql superuser

蓝咒 提交于 2019-12-30 04:05:12
问题 After I installed PostgreSQL 9.1 on Ubuntu 12.04 I set the password for the "postgres" superuser account. I want all users to have to enter their password when loging in. This is why I configured pg_hba.conf like so: #Database administrative login by Unix domain socket local all postgres md5 # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all md5 I restarted postgresql after making those changes. When I do this psql -U testuser I get asked

Update a table with a trigger after update

有些话、适合烂在心里 提交于 2019-12-29 08:55:31
问题 I have two tables batch (batch_id,start_date,end_date,batch_strength,is_locked) sem (user_id,is_active,no_of_days) I have executed the trigger procedure given below then update the table using query CREATE OR REPLACE FUNCTION em_batch_update() RETURNS trigger AS $em_sem_batch$ BEGIN UPDATE batch set is_locked='TRUE' where (start_date + (select no_of_days from sem WHERE is_active='TRUE' and user_id='OSEM') ) <= current_date; return NEW; END; $em_sem_batch$ LANGUAGE plpgsql; CREATE TRIGGER em

Can the Postgres data type NUMERIC store signed values?

此生再无相见时 提交于 2019-12-29 08:36:13
问题 In PostgreSQL, I would like to store signed values -999.9 - 9999.9 . Can I use numeric(5.1) for this? Or what type should I use? 回答1: You can certainly use the arbitrary precision type numeric with a precision of 5 and a scale of 1, just like @Simon commented, but without the syntax error. Use a comma( , ) instead of the dot ( . ) in the type modifier: SELECT numeric(5,1) '-999.9' AS nr_lower , numeric(5,1) '9999.9' AS nr_upper; nr_lower | nr_upper ----------+---------- -999.9 | 9999.9 The

Convert escaped Unicode character back to actual character in PostgreSQL

自古美人都是妖i 提交于 2019-12-29 01:14:49
问题 Is there a way how I can convert the following string back to the human-readable value? I have some external data where all non-ascii characters are escaped. Example strings: 16 StringProvider_111=Telefon\u00ED kontakty 17 StringProvider_116=Odpov\u011Bdn\u00E1 osoba Required Result: 16 StringProvider_111=Telefoní kontakty 17 StringProvider_116=Odpovědná osoba SQLFiddle The database has UTF8 encoding and collation cs_CZ.UTF-8 回答1: One old trick is using parser for this purpose: postgres=#

Trigger vs. check constraint

☆樱花仙子☆ 提交于 2019-12-28 18:14:25
问题 I want to add a field-level validation on a table. There is a field named "account_number" and this field should always pass a "luhn" check. I've found a function called "luhn_verify" that seems to work properly (google for it if you are interested). It returns a boolean. My question is: Are there any major performance advantages in PostgreSQL for using a trigger for this validation vs. a check constraint. Additional information: PostgreSQL 9.1 Table does not currently have an insert trigger,

How to find a table having a specific column in postgresql

半城伤御伤魂 提交于 2019-12-28 07:35:27
问题 I'm using PostgreSQL 9.1. I have the column name of a table. Is it possible to find the table(s) that has/have this column? If so, how? 回答1: you can query system catalogs: select c.relname from pg_class as c inner join pg_attribute as a on a.attrelid = c.oid where a.attname = <column name> and c.relkind = 'r' sql fiddle demo 回答2: You can also do select table_name from information_schema.columns where column_name = 'your_column_name' 回答3: I've used the query of @Roman Pekar as a base and added

Change type of varchar field to integer: “cannot be cast automatically to type integer”

久未见 提交于 2019-12-28 03:17:05
问题 I have a small table and a certain field contains the type " character varying ". I'm trying to change it to " Integer " but it gives an error that casting is not possible. Is there a way around this or should I just create another table and bring the records into it using a query. The field contains only integer values. 回答1: There is no implicit (automatic) cast from text or varchar to integer (i.e. you cannot pass a varchar to a function expecting integer or assign a varchar field to an