postgresql-9.2

Changing a column from string to string array in postgresql

邮差的信 提交于 2019-11-27 17:37:42
问题 The following is a snippet of a table called "containers". Column | Type | Modifiers --------------------+-----------------------------+--------------------------------- id | uuid | not null name | character varying(255) | products | character varying | default '{}'::character varying How can I alter the products column to "character varying[]" and the corresponding modifiers to default '{}'::character varying[] ? Essentially, I want to convert a string to a string array. Note the products

How do I get my rails app to use my postgresql db?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 17:18:53
问题 I installed prostgres to use with my rails4 app, and I am able to connect to the database server and create databases. Then I made the necessary changes to my Gemfile and config/database.yml to use postgres. Then I created a new app, and in the app I created a User model: $ rails generate model User name:string email:string invoke active_record create db/migrate/20130806145935_create_users.rb create app/models/user.rb invoke rspec create spec/models/user_spec.rb And then I did: $ bundle exec

PostgreSQL cannot begin/end transactions in PL/pgSQL

╄→尐↘猪︶ㄣ 提交于 2019-11-27 17:10:15
问题 I am seeking clarification of how to ensure an atomic transaction in a plpgsql function, and where the isolation level is set for this particular change to the database. In the plpgsql function shown below, I want to make sure that BOTH the deletion AND the insertion succeed. I am getting an error when I try to wrap them in a single transaction: ERROR: cannot begin/end transactions in PL/pgSQL . What happens during execution of the function below if another user has added a default behavior

Select from a table variable

落爺英雄遲暮 提交于 2019-11-27 15:48:08
I am trying to save the result of a SELECT query, pass it, and reuse it in another PL/pgSQL function: DECLARE table_holder my_table; --the type of table_holder is my_table; result text; BEGIN SELECT * INTO table_holder FROM table_holder ; result = another_function(table_holder); return result; END The code for another_function(table_holder my_table) , respectively: BEGIN RETURN QUERY SELECT col FROM table_holder where id = 1; END Is it possible to run a SELECT query on a variable? If not, is there a way to get around this limitation? I am using PostgreSQL 9.2. There are no "table variables" in

How to change the template database collection coding

我们两清 提交于 2019-11-27 11:35:29
问题 I want build new postgreSQL database by: CREATE DATABASE newdb WITH OWNER = postgres ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'zh_CN.UTF-8' CONNECTION LIMIT = -1; and the error is: ERROR: new collation (zh_CN.UTF-8) is incompatible with the collation of the template database (en_US.UTF8) HINT: Use the same collation as in the template database, or use template0 as template. How to change the template database collection? 回答1: From PostgreSQL documentation: Another common reason

Find duplicate rows with PostgreSQL

孤街浪徒 提交于 2019-11-27 10:36:18
We have a table of photos with the following columns: id, merchant_id, url this table contains duplicate values for the combination merchant_id, url . so it's possible that one row appears more several times. 234 some_merchant http://www.some-image-url.com/abscde1213 235 some_merchant http://www.some-image-url.com/abscde1213 236 some_merchant http://www.some-image-url.com/abscde1213 What is the best way to delete those duplications? (I use PostgreSQL 9.2 and Rails 3.) MatthewJ Here is my take on it. select * from ( SELECT id, ROW_NUMBER() OVER(PARTITION BY merchant_Id, url ORDER BY id asc) AS

Postgresql 9.2 pg_dump version mismatch

走远了吗. 提交于 2019-11-27 09:11:48
问题 I am trying to dump a Postgresql database using the pg_dump tool. $ pg_dump books > books.out How ever i am getting this error. pg_dump: server version: 9.2.1; pg_dump version: 9.1.6 pg_dump: aborting because of server version mismatch The --ignore-version option is now deprecated and really would not be a a solution to my issue even if it had worked. How can I upgrade pg_dump to resolve this issue? 回答1: You can either install PostgreSQL 9.2.1 in the pg_dump client machine or just copy the

How to write a function that returns text or integer values?

怎甘沉沦 提交于 2019-11-27 07:11:57
问题 I'm using PostgreSQL 9.2.4. postgres=# select version(); version ------------------------------------------------------------- PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 64-bit (1 row) sqlfiddle link My Query executes the insertion safely. What i need is that my function should return something except the void datatype. Something like text("inserted into table") or integer(0-false,1-true) , it will be useful for me to validate whether it is inserted or not? I need a syntax for a

Find out which schema based on table values

醉酒当歌 提交于 2019-11-27 07:08:02
问题 My database is separated into schemas based on clients (i.e.: each client has their own schema, with same data structure). I also happen to have an external action that does not know which schema it should target. It comes from another part of the system that has no concepts of clients and does not know in which client's set it is operating. Before I process it, I have to find out which schema that request needs to target To find the right schema, I have to find out which holds the record R

Failed to find conversion function from unknown to text

久未见 提交于 2019-11-27 06:33:58
问题 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