postgresql-9.3

Invalid count and sum in cross tab query using PostgreSQL

你离开我真会死。 提交于 2019-12-01 08:39:23
I am using PostgreSQL 9.3 version database. I have a situation where I want to count the number of products sales and sum the amount of product and also want to show the cities in a column where the product have sale. Example Setup create table products ( name varchar(20), price integer, city varchar(20) ); insert into products values ('P1',1200,'London'), ('P1',100,'Melborun'), ('P1',1400,'Moscow'), ('P2',1560,'Munich'), ('P2',2300,'Shunghai'), ('P2',3000,'Dubai'); Crosstab query : select * from crosstab ( 'select name,count(*),sum(price),city,count(city) from products group by name,city

Convert value from string representation in base N to numeric

拟墨画扇 提交于 2019-12-01 08:23:46
问题 On my database i keep a numbers in specific notation as varchar values. Is there any way to typecast this values into decimals with selected notation? What i basically looking here should look like this: SELECT to_integer_with_notation('d', '20') int4 --------- 13 One more example: SELECT to_integer_with_notation('d3', '23') int4 --------- 302 回答1: Unfortunately, there is no built-in function for that in PostgreSQL, but can be written fairly easy: CREATE OR REPLACE FUNCTION number_from_base

How to insert multiple rows using a function in PostgreSQL

大城市里の小女人 提交于 2019-12-01 07:37:59
I want to insert more than one row in a table with function in PostgreSQL. This is my table CREATE TABLE mahasiswa ( nim CHAR(10), nama VACHAR(40) CONSTRAINT pk_nim PRIMARY KEY (nim) ) ; and this is the function I created CREATE FUNCTION insertdata(CHAR(10),varchar(40)) RETURNS VOID AS $$ INSERT INTO mahasiswa VALUES ($1,$2); $$ LANGUAGE 'sql'; When I call the function like this SELECT insertdata ('1234567890','Nahrun'), ('0987654321','Hartono'); only one row is inserted. How can I modify my function to insert more than one row at a time? Erwin Brandstetter The function you have should rather

Check if key exists in a JSON with PL/pgSQL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 07:34:19
I'm trying to check if a key exists in a JSON sent as parameter in a PL/pgSQL function. Here is the function. CREATE FUNCTION sp_update_user(user_info json) RETURNS json LANGUAGE plpgsql AS $$ BEGIN PERFORM user_info->>'lastname'; IF FOUND THEN UPDATE users SET lastname = user_info->>'lastname' WHERE id = sp_update_user.user_id; END IF; PERFORM user_info->>'firstname'; IF FOUND THEN UPDATE users SET firstname = user_info->>'firstname' WHERE id = sp_update_user.user_id; END IF; RETURN row_to_json(row) FROM (SELECT true AS success) row; END;$$; I tried with the PERFORM -clause, but even if the

Trigger with dynamic field name

坚强是说给别人听的谎言 提交于 2019-12-01 06:43:15
I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema that's included in your search_path : How to use % operator from the extension pg_trgm? Best way to install

Wrong JDBC driver being used?

a 夏天 提交于 2019-12-01 06:30:34
I have a method that inserts a record into a Postgres DB and returns the identity field generated for said record. The problem is, if I include the Redshift driver in my POM file, that driver is getting used instead of the Postgres driver - and the Redshift driver doesn't allow returning the identity value. The code is: try { Class.forName( "org.postgresql.Driver" ).newInstance(); Connection connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5433/postgres", "postgres", "password" ); Statement stmt = connection.createStatement(); stmt.execute( "insert into public.job ( job

How to insert multiple rows using a function in PostgreSQL

不想你离开。 提交于 2019-12-01 03:56:45
问题 I want to insert more than one row in a table with function in PostgreSQL. This is my table CREATE TABLE mahasiswa ( nim CHAR(10), nama VACHAR(40) CONSTRAINT pk_nim PRIMARY KEY (nim) ) ; and this is the function I created CREATE FUNCTION insertdata(CHAR(10),varchar(40)) RETURNS VOID AS $$ INSERT INTO mahasiswa VALUES ($1,$2); $$ LANGUAGE 'sql'; When I call the function like this SELECT insertdata ('1234567890','Nahrun'), ('0987654321','Hartono'); only one row is inserted. How can I modify my

refresh materialized view periodically postgres

做~自己de王妃 提交于 2019-12-01 03:54:20
for optimization purposes I'm using a materialized view, to refresh it periodically I have set a cron job that runs each period t in my case every three hours. my questions are: what's the best way to refresh a materialized view? what can go wrong if using a cron job to refresh a materialized view? I have come across a postgres plugin that schedule jobs link the best way is to execute periodically a script that does the task: the script is: #!/bin/sh psql -U user_name -d database_instance_name -c 'refresh materialized view view_name' and add an entry in the crontab like: @hourly /full_path

Inserting valid json with copy into postgres table

不打扰是莪最后的温柔 提交于 2019-12-01 03:09:45
Valid JSON can naturally have the backslash character: \. When you insert data in a SQL statement like so: sidharth=# create temp table foo(data json); CREATE TABLE sidharth=# insert into foo values( '{"foo":"bar", "bam": "{\"mary\": \"had a lamb\"}" }'); INSERT 0 1 sidharth=# select * from foo; data \----------------------------------------------------- {"foo":"bar", "bam": "{\"mary\": \"had a lamb\"}" } (1 row) Things work fine. But if I copy the JSON to a file and run the copy command I get: sidharth=# \copy foo from './tests/foo' (format text); ERROR: invalid input syntax for type json

Installing PostgreSQL within a docker container

核能气质少年 提交于 2019-11-30 22:09:31
问题 I've been following several different tutorials as well as the official one however whenever I try to install PostgreSQL within a container I get the following message afterwards psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? I've looked through several questions here on SO and throughout the internet but no luck. 回答1: The problem is that the your application