postgresql-8.4

Importing CSV and commas in string values

两盒软妹~` 提交于 2019-12-07 12:25:45
问题 I use postgres 8.4 and currently trying to import a trivial CSV: Here is a table: CREATE TABLE public.sample ( a VARCHAR, b VARCHAR ) WITHOUT OIDS; and here is a csv file sample: "foo","bar, baz" The query COPY sample FROM '/tmp/sample.csv' USING DELIMITERS ','; expected throws ERROR: extra data after last expected column CONTEXT: COPY sample, line 1: ""foo","bar, baz"" But, well, CSV parsing is not a rocket science and I would wonder if it is not possible to solve without reformatting the

Database table size did not decrease proportionately

穿精又带淫゛_ 提交于 2019-12-06 20:22:34
问题 I am working with a PostgreSQL 8.4.13 database. Recently I had around around 86.5 million records in a table. I deleted almost all of them - only 5000 records are left. I ran reindex and vacuum analyze after deleting the rows. But I still see that the table is occupying a large disk space: jbossql=> SELECT pg_size_pretty(pg_total_relation_size('my_table')); pg_size_pretty ---------------- 7673 MB Also, the index value of the remaining rows are pretty high still - like in the million range. I

Create a temporary table from a selection or insert if table already exist

孤者浪人 提交于 2019-12-06 18:35:06
问题 What is the best way to create a temporary table, if it does not already exist, and add the selected rows to it? 回答1: CREATE TABLE AS is the simplest and fastest way: CREATE TEMP TABLE tbl AS SELECT * FROM tbl WHERE ... ; Not sure whether table already exists CREATE TABLE IF NOT EXISTS ... was introduced in version Postgres 9.1. For older versions, use the function provided in this related answer: PostgreSQL create table if not exists Then: INSERT INTO tbl (col1, col2, ...) SELECT col1, col2,

Find PostgreSQL server hostname on which it runs

落花浮王杯 提交于 2019-12-06 15:37:57
Is it possible to find the host on which the PostgreSQL Server runs ? Use SELECT inet_server_addr() , but it'll return NULL if connection is established via Unix-domain socket. And there's inet_server_port() which returns the port server is listening. 来源: https://stackoverflow.com/questions/14039101/find-postgresql-server-hostname-on-which-it-runs

Sending empty values with PDO results in error

别说谁变了你拦得住时间么 提交于 2019-12-06 13:27:12
We have something like the following PDO Statement which we use to communicate with a PostgreSQL 8.4 DB. $st = $db -> prepare("INSERT INTO Saba.Betriebskosten (personalkosten) VALUES(:kd_personalkosten)"); $st -> bindParam(':kd_personalkosten', $val['kd_personalkosten']); $val['kd_personalkosten'] is either empty/null or contains a double value. In the case it is empty/null, we just want to insert an empty value, but we receive the following error: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type double precision: ''; Which means that empty/null is converted

Importing CSV and commas in string values

。_饼干妹妹 提交于 2019-12-06 02:05:36
I use postgres 8.4 and currently trying to import a trivial CSV: Here is a table: CREATE TABLE public.sample ( a VARCHAR, b VARCHAR ) WITHOUT OIDS; and here is a csv file sample: "foo","bar, baz" The query COPY sample FROM '/tmp/sample.csv' USING DELIMITERS ','; expected throws ERROR: extra data after last expected column CONTEXT: COPY sample, line 1: ""foo","bar, baz"" But, well, CSV parsing is not a rocket science and I would wonder if it is not possible to solve without reformatting the source csv. Any ideas? PS: the csv comes from 3rd party, I cannot change the format. PPS: yes, I

Error “invalid byte sequence” while restoring PostgreSQL database

孤街醉人 提交于 2019-12-05 03:44:23
问题 Earlier today, I was trying to restore my PostgreSQL (8.1.22) database from production using pgAdmin III. However, after the restoration procedure finished, it started throwing errors like: WARNING: errors ignored on restore: 4 Also, upon investigation I found that out of all the tables 3 tables hadn't been restored (contained 0 rows). When I checked the log, I found the follwoing error near the 3 tables: pg_restore: [archiver (db)] Error from TOC entry 5390; 0 442375 TABLE DATA tablename

Aggregate strings in descending order in a PostgreSQL query

a 夏天 提交于 2019-12-05 00:09:13
In addition to the question How to concatenate strings of a string field in a PostgreSQL 'group by' query? How can I sort employee in descending order? I am using PostgreSQL 8.4 which doesn't support string_agg() . I've tried to use the following, but it isn't supported: array_to_string(array_agg(employee ORDER BY employee DESC), ',') I'd appreciate any hint to the right answer. In PostgreSQL 9.0 or later you can order elements inside aggregate functions : SELECT company_id, array_agg(employee ORDER BY company_id DESC)::text FROM tbl GROUP BY 1; That's not available for PostgreSQL 8.4 . You

Postgres - Passing table name as parameter and store result in file

∥☆過路亽.° 提交于 2019-12-04 06:44:18
问题 Create Or Replace Function totalRecords (tablename TEXT) Returns integer as $total$ Declare total integer; Begin select count (*) into total from''|| tablename ||' 'where now() - cast(date_dimension_year||'-'||date_dimension_month||'-'||date_dimension_day AS date) < INTERVAL '3 months' ; RETURN total; END; $total$ LANGUAGE plpgsql; i have a task which is to create a function which checks the DB for records on given condition if satisfied should output the result to a text file.The above

Jump SQL gap over specific condition & proper lead() usage

岁酱吖の 提交于 2019-12-04 02:03:59
问题 (PostgreSQL 8.4) Continuing with my previous example, I wish to further my understanding of gaps-and-islands processing with Window-functions. Consider the following table and data: CREATE TABLE T1 ( id SERIAL PRIMARY KEY, val INT, -- some device status INT -- 0=OFF, 1=ON ); INSERT INTO T1 (val, status) VALUES (10, 0); INSERT INTO T1 (val, status) VALUES (11, 0); INSERT INTO T1 (val, status) VALUES (11, 1); INSERT INTO T1 (val, status) VALUES (10, 1); INSERT INTO T1 (val, status) VALUES (11,