Menu
  • HOME
  • TAGS

PostgreSQL: trigger to call function with parameters

postgresql

A trigger procedure is created with the CREATE FUNCTION command, declaring it as a function with no arguments and a return type of trigger. You can use the arguments passed to the trigger function via TG_ARGV, e.g. TG_TABLE_NAME - the name of the table that caused the trigger invocation....

PostgreSQL conditional statement

sql,postgresql,if-statement

Try this: with c as (select count(*) cnt from table1) select table2.* from table2, c where c.cnt < 1 union all select table3.* from table3, c where c.cnt >= 1 ...

Subtract hours from the now() function

sql,postgresql,datetime,timezone,date-arithmetic

Answer for timestamp You need to understand the nature of the data types timestamp without time zone and timestamp with time zone (names can be deceiving). If you don't, read this first: Ignoring timezones altogether in Rails and PostgreSQL The AT TIME ZONE construct transforms your timestamp to timestamptz, which...

Postgres SQL constraint a character type

postgresql,constraints

Use a check constraint: CREATE TABLE my_table ( id character varying(255) NOT NULL, uid character varying(255) NOT NULL, my_text text NOT NULL, is_enabled boolean NOT NULL, constraint check_allowed check (my_text in ('A', 'B', 'C')) ); More details in the manual: http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS...

How to delete replication slot in postgres 9.4

postgresql,replication,postgresql-9.4

Use pg_drop_replication_slot: select pg_drop_replication_slot('bottledwater'); See the docs and this blog. The replication slot must be inactive, i.e. no active connections. So if there's a streaming replica using the slot you must stop the streaming replica. Or you can change its recovery.conf so it doesn't use a slot anymore and restart...

PostgreSQL How to find where function is being used

sql,postgresql

Assuming you know it's a trigger function (i.e. RETURNS TRIGGER), this should do it: SELECT tgname, tgrelid::regclass FROM pg_trigger WHERE tgfoid = 'func1'::regproc If func1 is overloaded, you would need to use e.g. tgfoid = 'func1(text,text)'::regprocedure. But in general, it might also appear in pg_aggregate, or pg_cast, or in a...

Save a hex-string to PostgreSQL column character varying

postgresql,hex

If you are sure that there are never more than 10 elements, you can simply cast your hex string to text: INSERT INTO my_table (hex_text) VALUES (<some hex data>::text); Or use a bytea column instead?...

postgresql complex group by in query

sql,postgresql

SELECT itemid, deadlineneeded, sum(quantity) AS total_quantity FROM <your table> WHERE (deadlineneeded - delievrydate)::int >= 1 GROUP BY 1, 2 ORDER BY 1, 2; This uses a "delievrydate" (looks like a typo to me) that is at least 1 day before the "deadlineneeded" date, as your sample data is suggesting....

Empty array as postgresql array column defult value

sql,postgresql

You need to use the explicit array initializer and cast that to the correct type: ALTER TABLE public.accounts ALTER COLUMN pwd_history SET DEFAULT array[]::varchar[]; ...

Django 1.8 migration unable to cast column id to integer

django,postgresql,django-migrations

The problem is the migration from Process.milestone as a boolean field to Process.milestone as a foreign key. Postgres doesn't wait for a migration to fail on uncastable data. It wants a rule to alter the table in advance. If you don't intend any sort of data migration between two fields,...

How to avoid SQL Injection with Update command in Npgsql?

database,postgresql,sql-injection,npgsql

As @tadman says, you should never use string concatenation to compose your query - that is the source of SQL injection. However, there's no need to prepare your statement. Use parameter placeholders in your query, something like the following should work: string UpdateCmd = "update dx set chronic = @p1...

How to check what constraint has been violated?

java,sql,postgresql,exception

something like below catch (ConstraintViolationException conEx) { if (conEx.getConstraintName().contains("xyz_fK")) { //TODO Project Entity is violating it's constrain } LOGGER.Info( "My log message", conEx.getConstraintName()); LOGGER.ERROR( "My log message", conEx); ...

How to get second row in PostgreSQL?

sql,postgresql

This query uses WITH construction that works similar to sub-queries. Investigate this query with EXPLAIN before use in production because it may be slow on big tables: WITH orders AS ( SELECT email , first_value(dt_cr) OVER wnd1 AS min_date , nth_value(dt_cr, 2) OVER wnd1 AS second_date FROM orders WINDOW wnd1...

Calculations between 2 records of 2 tables need to extend to more then 1300 fields. How and which database can achieve this?

database,postgresql,database-design

As an example to my comment above, using an array rather than discrete fields for each value: create table tablea ( id int not null, a numeric[] ); create table tableb ( id int not null, b numeric[] ); insert into tablea select 2, array (select generate_series(1,10000)); insert into tableb...

How to group following rows by not unique value

sql,postgresql,greatest-n-per-group,window-functions,gaps-and-islands

If your case is as simple as the example values suggest, @Giorgos' answer serves nicely. However, that's typically not the case. If the id column is a serial, you cannot rely on the assumption that a row with an earlier time also has a smaller id. Also, time values (or...

Connecting C++ and Postgresql

c++,postgresql,linker-error,libpqxx

You need a makefile and you need to include your linker flag for pqxx. On my linux box the linker flag is -lpqxx. See my example makefile below. CXXFLAGS := LDFLAGS := -lpqxx # Executable output command $(EXE): $(OBJECTS) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o [email protected] $^ # build rule for c++...

when to use = and := in postgreSQL?

postgresql,triggers

In version 9.4, the documentation was updated to make it clear that there is no difference. Version 9.4: 40.5.1. Assignment An assignment of a value to a PL/pgSQL variable is written as: variable { := | = } expression; [...] Equal (=) can be used instead of PL/SQL-compliant := In...

Translation of interval

postgresql,datetime,translation,intervals,postgresql-8.4

I think no. sorry. day, month etc are field in type interval. it will be in English. like you don't expect use "vybrac" instead of select :) But you can have locale in time values, yes. td=# set lc_time TO pl_PL; SET td=# SELECT to_char(to_timestamp (4::text, 'MM'), 'TMmon'); to_char ---------...

Is it possible to count all rows with the same id with COUNT?

sql,postgresql

If all you need is a count of the number of rows where player_id is 1, then you can do this: SELECT count(*) FROM your_table_name WHERE player_id = 1; If you want to count the number of rows for each player_id, then you will need to use a GROUP BY:...

unexpected end of function definition on PostgreSQL function

postgresql

You just need to add semicolons: CREATE OR REPLACE FUNCTION on_ai_myTable() RETURNS TRIGGER AS $$ BEGIN SELECT fn_name, count(*) + 1 FROM table_ebscb_spa_log02 WHERE time_type = 'Start' GROUP BY fn_name; RETURN NEW.fn_name; END $$ LANGUAGE plpgsql; ...

Column does not Exist in postgresql?

sql,postgresql,postgresql-9.1

You can DRY up the duplication of the projection with a CTE, and then use this in your WHERE predicate: WITH myCte AS ( select order_id , order_item_id , sku ,merchant_payable, order_created_at , case when name like 'Rise%' then amount-(((amount*12.14)/100)+ ((amount*3.08)/100) + 51.30) when name like 'Masha%' then amount-(((amount*9.10)/100)+ ((amount*3.08)/100)...

SQL CTE Syntax to DELETE / INSERT rows

sql,postgresql,common-table-expression,postgresql-9.2

Your question update made clear that you cannot do this in a single statement. Packed into CTEs of the same statement, both operations (INSERT and DELETE) would see the same snapshot of the table and execute virtually at the same time. I.e., the INSERT would still see all rows that...

postgres: using previous row value when current row value is null

postgresql

The below query fills empty values in the resultset of your original query. The method consists in splitting the data into partitions according to the number of empty values and selecting the first (non-empty) value from each partition (add * to the select to see how it works). WITH survey...

Django: Which database call is faster/better?

python,django,postgresql

request.user.user_profile makes an SQL query to get user profile, then user_profile.games_liked.all() makes second sql query to get liked games. So, form this point of view Game.objects.filter(likes=request.user) is better. You can check what sql was executed, in shell, by looking into connection.queries variable (available only if setting DEBUG=True) from django.db import...

PostgreSQL: check same long condition on two columns

sql,postgresql

Provided that you don't need it to use an index on x or y, the simplest way is probably: ...WHERE ARRAY[x,y] <@ ARRAY[0,1,4,6,7] ...

Is there a way to count a number of distinct values? [duplicate]

sql,postgresql,select,distinct-values

You can use the distinct modifier in the count function: SELECT COUNT(DISTINCT p_id) FROM mytable ...

Retrieve updated rows in AFTER UPDATE trigger Postgresql

postgresql,triggers,plpgsql

You can create a temporary table (so that it will visible only in the session). In the row level trigger you insert the rows into the temporary table, in the statement level trigger you select (and delete) from the temporary table.

Ubuntu 14.04 - An error occurred while installing pg (0.18.2), and Bundler cannot continue

ruby-on-rails,postgresql,ubuntu,heroku,sqlite3

Do you have PostgreSQL installed locally? If not, that might be the reason for that (having gem 'pg' is not enough to install it locally). You will have to run sudo apt-get install postgresql postgresql-contrib to install. You do not need it installed locally to push to Heroku though...as long...

What does `@@` mean in a PostgresSQL query?

postgresql

"Matches" Ah, it just means "matches" in the context of a full-text search. http://www.postgresql.org/docs/9.4/static/textsearch-intro.html#TEXTSEARCH-MATCHING...

How to order SQL query result on condition?

sql,postgresql,order,condition

Use CASE expression in ORDER BY clause: SELECT category FROM ( SELECT DISTINCT category FROM merchant ) t ORDER BY CASE WHEN category = 'General' THEN 0 ELSE 1 END, category ASC CASE guarantees that rows with General will be sorted first. The second argument orders the rest of the...

Error while trying to insert data using plpgsql

postgresql,timestamp,plpgsql

CURRENT_TIME is a reserved word (and a special function), you cannot use it as variable name. You don't need a variable here to begin with: CREATE OR REPLACE FUNCTION test_func(OUT pid bigint) AS $func$ BEGIN INSERT INTO "TEST"(created) VALUES (now()) RETURNING id INTO pid; END $func$ LANGUAGE plpgsql; now() is...

Do not include select columns in group by

sql,postgresql,group-by,subquery

You can achieve this with a window function which doesn't require everything to be grouped: select * from ( select addresses.phone, addresses.name, orders.order_number, count(orders.order_number) over (partition by addresses.phone) as cnt from orders inner join carts on orders.order_number = carts.id inner join address on carts.address_id = addresses.id ) t where cnt...

How do I make a query search in rails case insensitive?

ruby-on-rails,postgresql,ruby-on-rails-4,search

Because you are using postgresql: def self.search(query) where("description ilike ?", "%#{query}%") end Just use ilike instead of like. like/ilike documentation If you want to use =, both sides either UPPER or LOWER...

How to customize the configuration file of the official PostgreSQL docker image?

postgresql,docker

The postgres:9.4 image you've inherited from declares a volume at /var/lib/postgresql/data. This essentially means you can can't copy any files to that path in your image; the changes will be discarded. You have a few choices: You could just add your own configuration file as a volume at run-time with...

PostgreSQL: How to multiply two columns and display result in same query?

sql,postgresql

You can just repeat what you wrote: select Greatest(p.price,0) as newprice, sum(q.qty) as qty, Greatest(p.price,0) * sum(q.qty) as qty or, you can wrap select to from (PostgreSQL: using a calculated column in the same query) select tmp.newprice, tmp.qty, tmp.newprice * tmp.qty from ( select Greatest(p.price,0) as newprice, sum(q.qty) as qty,...

load the data from file using multi threading

java,postgresql

Writing something to a storage will prevent all other threads from writing to the same. You cannot simply make everything multithreaded. In this case all other parts Need to wait for the previous one to finish....

PostgreSQL How to add WHERE where is count() and GROUP

sql,postgresql

active is of type character varying, i.e. a string type. This should work: SELECT count(*), date_trunc('year', "createdAt") AS txn_year FROM tables WHERE active = '1' GROUP BY txn_year; ...

Speed up Min/Max operation on postgres with index for IN operator query

postgresql,postgresql-9.3

It turned out (please see comments), that this query: SELECT MIN(minimal) AS minimal FROM ( SELECT MIN("products"."shipping") AS minimal FROM "products" WHERE "products"."tag_id" IN (?,?,?,?,?,?,?) GROUP BY "tag_id" ) some_alias is able to deceive PostgreSQL in such a way, that it performs better because, as I guess, it uses the...

What happens with duplicates when inserting multiple rows?

sql,postgresql,exception,duplicates,upsert

The INSERT will just insert all rows and nothing special will happen, unless you have some kind of constraint disallowing duplicate / overlapping values (PRIMARY KEY, UNIQUE, CHECK or EXCLUDE constraint) - which you did not mention in your question. But that's what you are probably worried about. Assuming a...

How to get number of invoices for last 12 weeks in Postgres

sql,postgresql,asp.net-mvc-3,aggregate-functions,generate-series

CROSS JOIN the latest date to generate_series(), followed by a LEFT JOIN to the main table. SELECT ARRAY( SELECT count(d.invoicedate) AS ct FROM ( SELECT max(invoicedate) AS last_date FROM dok WHERE invoicedate < current_date -- "maximum date before current date" ) l CROSS JOIN generate_series(0, 11*7, 7) AS g(days) LEFT...

Spring Boot - How to set the default schema for PostgreSQL?

java,spring,hibernate,postgresql

You can try setting the default schema for the jdbc user. 1) ALTER USER user_name SET search_path to 'schema' 2) Did you try this property? spring.datasource.schema http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html...

Postgresql Iterate over an array field and use the records for another query

sql,arrays,postgresql,many-to-many

Since fleets is an array column you have a couple of options. Either use the ANY construct directly (no need to unnest()): SELECT * FROM vehicles WHERE fleet_fk = ANY(SELECT fleets FROM auth_user WHERE id = 4); Or rewrite as join: SELECT v.* FROM auth_user a JOIN vehicles v ON...

How to insert and Update simultaneously to PostgreSQL with sqoop command

postgresql,hadoop,hive,sqoop

According to my internet search, it is not possible to perform both insert and update directly to postgreSQL DB. Instead you can create a storedProc/function in postgreSQL and you can send data there.. sqoop export --connect <url> --call <upsert proc> --export-dir /results/bar_data Stored proc/function should perform both Update and Insert....

plv8 disadvantages or limitations?

postgresql,plpgsql,plv8

The advantages and disadvantages of PLV8 are same as advantages and disadvantages of PLPerl, PLPython and other PL languages. It is not integrated with PostgreSQL engine - the processing SQL statements result can be slower. PLpgSQL is fully integrated to PostgreSQL engine. SQL is not integrated to language - isn't...

Using psql options in npgsql

vb.net,postgresql,postgis,psql,npgsql

Npgsql is an simply a driver for interacting with the database, HTML formatting of results is completely out of its scope (unlike psql, which is a user-facing console app). Note that there wouldn't be any "universal" way to format the HTML in a way that would satisfy everyone. However, it...

PostgreSQL - aggregating data by date from datetime field

postgresql,datetime

Just cast it to a date: select sum(use), localminute::date from tablename group by localminute::date; or using standard SQL: select sum(use), cast(localminute as date) from tablename group by cast(localminute as date); ...

How to create a SELECT query FROM “TABLE1 AND TABLE2”

sql,postgresql,select,join

UNION ALL SELECT field1, field2, field3 FROM table1 WHERE condition UNION ALL SELECT field1, field2, field3 FROM table2 WHERE condition; Or to simplify your WHERE condition SELECT * FROM ( SELECT field1, field2, field3 FROM table1 UNION ALL SELECT field1, field2, field3 FROM table2 ) WHERE condition; ...

Return id from postgresql insertion

sql,postgresql,sql-insert

There are several ways to do it: Assuming a._id is a serial column: insert into a (data) values ('foo'); insert into b (_id, other_data) values (lastval(), 'foobar'); Edit (after discussion in comments): Note that lastval() is concurrency safe (as all sequence related functions). Even if other sessions insert something into...

Alternatives to WITH .. AS .. clause in PostgreSQL

sql,postgresql,plpgsql,common-table-expression,postgresql-9.3

The query in the question has a couple of obvious nonsense parts. Since you have been executing it before, I assume these are artifacts from manual simplification? Like: total * 100 / total would be pointless since it burns down to just 100. Or: joins without join condition, which are...

Formatting dates in PostgreSQL

sql,postgresql,select,date-formatting

You can use the to_char function to format your column: SELECT TO_CHAR(ddatte, 'dd/Mon/yyyy') FROM mytable ...

Connecting from Spark/pyspark to PostgreSQL

postgresql,jdbc,jar,apache-spark,pyspark

Remove spark-defaults.conf and add the SPARK_CLASSPATH to the system environment in python like this: os.environ["SPARK_CLASSPATH"] = 'PATH\\TO\\postgresql-9.3-1101.jdbc41.jar' ...

Postgresql Update JDBC

java,postgresql,jdbc

You shouldn't build SQL by putting your variables directly via string concatenation. What happens here is that with 11, your SQL becomes: set last=11 Which is valid SQL (using 11 as a integer literal), while with xx it becomes: set last=xx There are no quotes, so the SQL means you're...

In PostgreSQL is it possible to join between table and function?

sql,postgresql

Your table uses a carid value to retrieve the corresponding part_ids from function PartsPerCar() which returns a set of rows, a so-called table function. In sub-section 5 (keep on reading) we see that Table functions appearing in FROM can also be preceded by the key word LATERAL, but for functions...

name of value returned from PostgreSQL function

postgresql,plpgsql

You can choose between: select aschema.afunction() as my_name; -- like in IMSoP's answer select my_name from aschema.afunction() as my_name; -- with alias select afunction from aschema.afunction(); -- with function name If you add aschema to search path, you can omit schema identifier: set search_path to public, aschema; select afunction() as...

How to install postgres with NSIS with all parameters?

postgresql,installer,package,nsis

This is about right. Please do not use port 5432 for PostgreSQL. Pick a non-default port well outside that range, like 5599 or something. Change the port by modifying postgresql.conf and setting the port directive. You might find it useful to simply append include_dir = conf.d or similar in your...

PostgreSQL: Aggregate by more than one column

sql,postgresql,aggregate-functions

You need to convert eventtime to a date by removing the time component. I think the simplest way is using date_trunc(): select id, date_trunc('day', eventtime) as eventday, sum(use) from table group by id, eventday order by id, eventday; I'm not sure why your sample query has id in the select,...

Superuser Role Specific to certain Databases in PostgreSQL

database,postgresql,role,superuser

As @Craig explained, you can't (and even if you could, it would be fairly pointless). The usual way of implementing restricted superuser permissions is to connect as an existing superuser role, and create SECURITY DEFINER functions containing a limited set of approved commands. These functions will now be executed with...

what is the SQL prepared stament for lo_import in postgreSQL

c++,sql,database,postgresql,odbc

The query to prepare should be insert into test values(?,lo_import(?)); You proposal insert into test values(?,?) can't work because you can't submit a SQL function call (lo_import) as the value for a placeholder (?). Placeholders only fit where a literal value would fit. When you're asking what's the prepared statement...

JSONB: more than one row returned by a subquery used as an expression

sql,postgresql,postgresql-9.4,jsonb,set-returning-functions

The error means just what it says: more than one row returned by a subquery used as an expression The expression in the WHERE clause expects a single value (just like you substituted in your added test), but your subquery returns multiple rows. jsonb_array_elements() is a set-returning function. Assuming this...

(Automatically) convert SQL strings to 'enums'?

sql,performance,postgresql

There is no built-in facility to do what you want, at least in PostgreSQL. Doing it effectively would require signifciant changes to how data is stored, as currently each row is independent of all other rows (well, except TOAST pointers for out-of-line stored data that's unchanged in an UPDATE). A...

Subtract two columns of different tables

sql,postgresql,sum,aggregate-functions,subtract

I interpret your remark but that result can't to be negative as requirement to return 0 instead of negative results. The simple solution is GREATEST(): SELECT GREATEST(sum(amount) - (SELECT sum(amount) FROM solicitude WHERE status_id = 1 AND user_id = 1), 0) AS total FROM contribution WHERE user_id = 1; Otherwise,...

Syntax error while creating table in PostgreSQL 8.1

postgresql

PostgreSQL 8.1 only supports INCLUDING DEFAULTS. You'll either have to upgrade to at least 8.3 or create the indices manually....

Is there a better solution to join the same dataset to get the distinct count of a dimension used for aggregation?

sql,postgresql

You can simply do a Group Count on the result of the aggregation: SELECT dim1, dim2, COUNT(*) OVER (PARTITION BY dim1), SUM(measure1) measure1, SUM(measure2) measure2 FROM test GROUP BY dim1, dim2 ...

PostgreSQL can't find column

java,postgresql

Just in case the issue it related to upper and lower case in the column name: it's possible to put the column name in double quotes: PreparedStatement ps = conn.prepareStatement("SELECT * FROM produits where \"NOM_PRODUIT\" like ?"); This way the name is case sensitive....

Sqoop Export with Missing Data

sql,postgresql,shell,hadoop,sqoop

I solved the problem by changing my reduce function so that if there were not the correct amount of fields to output a certain value and then I was able to use the --input-null-non-string with that value and it worked.

rails block a record for change for another places in the code

ruby-on-rails,ruby,postgresql,activerecord,sidekiq

You need to lock the model: account = Account.first account.with_lock do # This block is called within a transaction, # account is already locked. account.balance -= 100 account.save! end You can read more about it here: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html...

Update enum column in Laravel migration using PostgreSQL

postgresql,laravel,laravel-5,laravel-migrations

Laravel use constraint on character varying for enum. Assuming there is a table mytable with an enum column status, we have to drop the constraint (named tablename_columnname_check) then add it in a migration like this: DB::transaction(function () { DB::statement('ALTER TABLE mytable DROP CONSTRAINT mytable_status_check;'); DB::statement('ALTER TABLE mytable ADD CONSTRAINT mytable_status_check...

Redirect if ActiveRecord::RecordNotUnique error exists

ruby-on-rails,postgresql,activerecord,error-handling

Just the way you catch every other error begin Transaction.create!(:status => params[:st], :transaction_id => params[:tx], :purchased_at => Time.now) rescue ActiveRecord::RecordNotUnique redirect_to root_path end ...

Prepared statements: Using unnamed and unnumbered question mark style positional placeholders

postgresql

Prepared statements are used to speed up the repeated execution of the same query with different arguments. If your aim is to insert many rows at once it is better to execute regular insert query, which will be faster than the prepared insert. However, if you insisted on this solution,...

postgresql mathematical formula error

postgresql

You are trying to use COUNT(sale_order_line.name) as a group by item. Aggreagte functions work on grouped item. They are not for grouping them. I do not know your tables but try Select stock_inventory_line.product_code AS Sku, COUNT(sale_order_line.name) AS Qty_Sold, stock_inventory_line.product_qty AS Current_Qty, (stock_inventory_line.product_qty / COUNT(sale_order_line.name)) AS NOM From sale_order_line, product_product, product_template,...

heroku pgbackups:url command is no longer working?

ruby-on-rails,postgresql,ruby-on-rails-4,amazon-web-services,heroku

To download backup b004 use the following syntax: curl -o b004.dump `heroku pg:backups public-url b004` That will download the backup as b004.dump in the current directory....

Avoid calling COUNT twice in CASE expression (PostgreSQL)

sql,postgresql

This expression: CASE COUNT(measurement.id) > 1 THEN to_char(COUNT(measurement.id),' 999') ELSE '' is not slow because COUNT() is called twice. The hard work of aggregating the data is the part where the key values are brought together. The individual aggregation functions are generally not particularly expensive (there are exceptions such as...

How to select rows by time interval in PostgreSQL?

sql,postgresql

Use the following to get what your after. SELECT TIMESTAMP WITH TIME ZONE 'epoch' + INTERVAL '1 second' * round((extract('epoch' FROM timestamp) / 1800) * 1800) AS clock, SUM(usage) FROM my_table WHERE timestamp > localtimestamp - INTERVAL '1 week' GROUP BY round(extract('epoch' FROM timestamp) / 1800) ...

JAVA postrgresql insert String[] to text[] postrgresql cell

java,postgresql

My advice is not to build you SQL statements through concatenation of strings as you are doing. I would use the PreparedStatement class that brings methods to deal with so many different types, as arrays, and it brings additional benefits as protection from value injections from evil users. For instance,...

How to install / use orafce package in postgresql 9.4?

postgresql,debian,orafce

Ok, a smple CREATE EXTENSION orafce is enough...

need help specifying potentially reserved words as strings in postgres query

postgresql

For string literals, you should you single quote instead of double quote: UPDATE rv_template_fields SET view = 'display_type_1' WHERE rv_template_fields.view = 'display_type_2' Double quotes are for quoting identifiers of fields and relations, like, for instance view, so that you could write also: UPDATE rv_template_fields SET "view" = 'display_type_1' WHERE "view"...

postgresql run multiple select statements with different columns in single query

sql,postgresql

Just pad the columns you don't have and union. For instance: select activities.type, sum(activity_fees.amount) ... Union SELECT 'dummy', avg(activities.rating) .... Or just include activities.type since you have it available!...

postgresql query json for value (instead of key)?

json,postgresql

I do not think there is an operator which you can use in such a simple query. You can achieve this with the query below, though I do not know whether it is the simplest way: select distinct on (id) id, data from ( select id, data, (json_each(data)).value::text from json_table...

How to use Rails #update_attribute with array field?

ruby-on-rails,ruby,postgresql,ruby-on-rails-4,activerecord

I don't think update_attribute is going to be useful as it will replace the array with the new value rather than append to it (but see better explanation below in --Update-- section). I'm not sure what best practices are here, but this should work to just add something if it...

why do we use vaccum command in PostgreSQL exactly?

postgresql

If you DELETE rows or UPDATE them, then VACUUM is required to free the space for re-use. PostgreSQL usually does this automatically with autovacuum, so it is not common for you to need to manually run VACUUM. You might manually run VACUUM after updating a large proportion of a table,...

PostgreSQL order by TSRange

postgresql

There are range functions described in documentation. SELECT * FROM my_table ORDER BY lower(range_column); ...

JPA NamedNativeQuery syntax error with Hibernate, PostgreSQL 9

java,hibernate,postgresql,jpa

So the problem was that em.createNativeQuery(...) was not the correct invocation of a NamedNativeQuery in order to do that I should've invoked em.createNamedQuery(...). However, seeing that em.createNativeQuery(...) does not accept @SqlResultSetMapping it is very difficult to map the result to a custom class. The end solution was to use return...

Return integer value of age(date) function in Postgres

postgresql

Here is how you get the number of days comparing two dates: SQL> select extract(day from now()-'2015-02-21'::timestamptz); date_part ----------- 122 (1 row) ...

what is “DESC LIMIT 1” used for in plpgsql?

postgresql,triggers

It is not really DESC LIMIT 1, the DESC is connected to ORDER BY t.timestamp02 DESC and LIMIT 1 is a second clause. The documentation will tell you, that DESC will cause the ORDER to order in descending order, and LIMIT 1 will limit the resultset to 1 item. In...

rails db:migrate fails rake aborted ActiveRecord::NoDatabaseError

ruby-on-rails,postgresql,heroku

Postgresql comes preinstalled on Cloud9. You simply can't run bundle exec rake db:migrate though, because you have to set it up and connect to it first. Refer to the documentation here https://docs.c9.io/v1.0/docs/setting-up-postgresql on how to set it up. Also, you dont need to install postgresql to be able to deploy...

PostgreSQL function execute query

sql,postgresql,plpgsql,postgresql-9.3

The error is actually not about the condition or anything in the function itself, but the syntax of the function creation. You start the function definition with $func$ and end it with $$. This will not work. Change the $func$ to $$ to fix the syntax. ...

INSERT a number in a column based on other columns OLD INSERTs

postgresql,triggers,autofill

In an AFTER INSERT trigger, any changes you make to NEW.time_index will be ignored. The record is already inserted at this point; it's too late to modify it. Create the trigger as BEFORE INSERT instead....

Postgres Index-only-scan: can we ignore the visibility map or avoid heap fetches?

postgresql,indexing

There is no way to do what you want in PostgreSQL as it stands. It'd be interesting to do but a fair bit of work, very unlikely to be accepted into core, extremely hard to do with an extension, and likely to have worse side-effects than you probably expect. You'd...

Struggling with a simple JOIN

sql,postgresql

This is a bit tricky, because it is more than a "simple join". There are several ways to approach this. One method uses aggregation and a having clause: select op.order_id from order_products op group by op.order_id having sum(case when op.status = 'shipped' then 1 else 0 end) = count(*); That...

JDBC - PostgreSQL - batch insert + unique index

java,sql,database,postgresql,jdbc

The most efficient way of doing this would be something like this: create a staging table with the same structure as the target table but without the unique constraint batch insert all rows into that staging table. The most efficient way is to use copy or use the CopyManager (although...

flask-sqlalchemy, sql count records in a relationship and then join to a users table

python,postgresql,flask-sqlalchemy

select users.*, count(*) over (partition by friendship.user) from users inner join friendship on users.id = friendship.user order by count ...

cast uuid to varchar in postgres when I use sequelize

postgresql,uuid,sequelize.js

I write the query condition as follows, and it works well. Is there any better solution? {where: ['_id::"varchar" like ?', '%1010%']}, ...

Aggregate count by several weeks after field data in PostgreSQL

postgresql,aggregate

You can use extract(days from (action_at - registered_at) / 7)+1 to get the number of weeks. Then count the number of actions grouped by the number of weeks. select user_id, wk, count(*) actions from (select user_id, extract(days from (action_at - registered_at) / 7)+1 wk from Table1) a where wk <=...

group by is not working in postgreSQL

sql,postgresql,group-by

ID is unique and group by ID works just like a plain select. Column createdAt is not unique and results with same createdAt value must be grouped. You should provide a way how they will be grouped - use aggreagete function, remove them from select clause or add them to...

Import csv file and update existing table in Postgres

postgresql,csv,heroku-postgres,csv-import

SQL>begin; BEGIN Time: 0.366 ms SQL>truncate table t; TRUNCATE TABLE Time: 3.068 ms SQL>select * from t; t --- (0 rows) Time: 2.844 ms SQL>copy t from '/tmp/t'; COPY 2 Time: 1.693 ms SQL>select * from t; t ------------------------------- 2014-10-09 08:09:58.241592+00 2015-06-17 09:18:05.731139+00 (2 rows) Time: 1.823 ms SQL>end; COMMIT...