postgresql-9.1

to_char(number) function in postgres

只谈情不闲聊 提交于 2019-11-29 02:02:30
问题 i want to display/convert a number to character (of it's same length) using to_char() function . In oracle i can write like SELECT to_char(1234) FROM DUAL But in postgres SELECT to_char(1234) is not working. 回答1: You need to supply a format mask. In PostgreSQL there is no default: select to_char(1234, 'FM9999'); If you don't know how many digits there are, just estimate the maximum: select to_char(1234, 'FM999999999999999999'); If the number has less digits, this won't have any side effects.

Postgres SELECT … FOR UPDATE in functions

流过昼夜 提交于 2019-11-29 01:37:23
问题 I have two questions about using SELECT … FOR UPDATE row-level locking in a Postgres function: Does it matter which columns I select? Do they have any relation to what data I need to lock and then update? SELECT * FROM table WHERE x=y FOR UPDATE; vs SELECT 1 FROM table WHERE x=y FOR UPDATE; I can't do a select in a function without saving the data somewhere, so I save to a dummy variable. This seems hacky; is it the right way to do things? Here is my function: CREATE OR REPLACE FUNCTION

now() default values are all showing same timestamp

前提是你 提交于 2019-11-29 00:30:52
问题 I have created my tables with a column (type: timestamp with timezone) and set its default value to now() ( current_timestamp() ). I run a series of inserts in separate statements in a single function and I noticed all the timestamps are equal down to the (ms), is the function value somehow cached and shared for the entire function call or transaction? 回答1: That is expected and documented behaviour: From the manual: Since these functions return the start time of the current transaction, their

PostgreSQL 9.1: How to concatenate rows in array without duplicates, JOIN another table

≡放荡痞女 提交于 2019-11-28 20:14:54
I am using PostgreSQL 9.1 and need help with concatenating multiple rows in one. I need to do that in 2 tables. When I use two times array_agg() functions I get duplicated values in result. Tables: CREATE TABLE rnp (id int, grp_id int, cabinets varchar(15) ); INSERT INTO rnp VALUES (1,'11','cabs1') ,(2,'11','cabs2') ,(3,'11','cabs3') ,(4,'11','cabs4') ,(5,'22','c1') ,(6,'22','c2'); CREATE TABLE ips (id int, grp_id int, address varchar(15)); INSERT INTO ips VALUES (1,'11','NY') ,(2,'11','CA') ,(3,'22','DC') ,(4,'22','LA'); SQL: SELECT DISTINCT rnp.grp_id, array_to_string(array_agg(rnp.cabinets

Determining the OID of a table in Postgres 9.1?

核能气质少年 提交于 2019-11-28 20:14:16
Does anyone know how to find the OID of a table in Postgres 9.1? I am writing an update script that needs to test for the existence of a column in a table before it tries to create the column. This is to prevent run of the script after the first from erroring out. The postgres catalog table pg_class is what you should look at. There should be one row per table, with the table name in the column relname , and the oid in the hidden column oid . The catalog tables are in the postgres database, so make sure to connect to that, rather than your application database. You may also be interested in

How to change schema of multiple PostgreSQL tables in one operation?

99封情书 提交于 2019-11-28 17:04:40
I have a PostgreSQL 9.1 database with 100 or so tables that were loaded into the 'public' schema. I would like to move those tables (but not all of the functions in 'public') to a 'data' schema. I know that I can use the following to move 1 table at a time. ALTER TABLE [tablename] SET SCHEMA [new_schema] Is it possible to move all of the tables to the new schema in one operation? If so, what would be the most efficient way to accomplish this task? DO will do the trick: DO $$ DECLARE row record; BEGIN FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = 'public' -- and other conditions

Query a parameter (postgresql.conf setting) like “max_connections”

早过忘川 提交于 2019-11-28 15:37:20
Does anyone know if it's even possible (and how, if yes) to query a database server setting in PostgreSQL (9.1)? I need to check the max_connections (maximum number of open db connections) setting. Can be as simple as: SHOW max_connections; This returns the currently effective setting. Be aware that it can differ from the setting in postgresql.conf as there are a couple of ways to set run-time parameters in PostgreSQL . To reset the "original" setting from postgresql.conf in your current session: RESET max_connections; However, not applicable to this particular setting. Per documentation :

List tables in a PostgreSQL schema

佐手、 提交于 2019-11-28 13:20:25
问题 When I do a \dt in psql I only get a listing of tables in the current schema ( public by default). How can I get a list of all tables in all schemas or a particular schema? 回答1: In all schemas: => \dt *.* In a particular schema: => \dt public.* It is possible to use regular expressions with some restrictions \dt (public|s).(s|t) List of relations Schema | Name | Type | Owner --------+------+-------+------- public | s | table | cpn public | t | table | cpn s | t | table | cpn Advanced users

Convert escaped Unicode character back to actual character in PostgreSQL

狂风中的少年 提交于 2019-11-28 12:48:13
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 One old trick is using parser for this purpose: postgres=# select e'Telefon\u00ED kontakty'; ?column? ------------------- Telefoní kontakty (1 row) CREATE OR REPLACE

Failed to find conversion function from unknown to text

怎甘沉沦 提交于 2019-11-28 11:52:13
In one of my select statements I've got the following error: ERROR: failed to find conversion function from unknown to text ********** Error ********** ERROR: failed to find conversion function from unknown to text SQL state: XX000 This was easy to fix using cast , but I do not fully understand why it happened. I will ilustrate my confusion with two simple statements. This one is OK: select 'text' union all select 'text'; This will return error: with t as (select 'text') select * from t union all select 'text' I know I can fix it easily: with t as (select 'text'::text) select * from t union