Quantcast
Channel: Question and Answer » postgresql
Viewing all 1138 articles
Browse latest View live

Howto disable Postgres listening on TCP?

$
0
0

Simply to avoid many problems in the first place I do not want my postgres server program to accept/listen to anything from any network (i.e. TCP/IP 4/6) connections.

My setup is a Postgres 9.1 on an Ubuntu 12.04 box and I thought tweeking /etc/postgresql/9.1/main/pg_hba.conf to not include those lines which commented out (see below) would cause postgres to “please not listen on network TCP/IP devices”

local   all             postgres        trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
#local   all             all                                     md5
# IPv4 local connections:
#host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
#host    all             all             ::1/128                 md5

Also I know that there is the -i command line to start the postgres server with if we YES want to listen on TCP/IP . I actually seek for the opposite thing a -??? meaning NO please do NOT listen on TCP/IP.

I used a netstat -utap | grep post and it shows that postgres besides my settings in /etc/postgresql/9.1/main/pg_hba.conf is still listening on TCP/IP.

QUESTION

What did I do wrong here? How can I shut off this TCP/IP listening attitude of my Postgres server?
Having only unix socket listening I am happy to the max ;)

Thank you

Addition: I also perceive that postgres establishes a UDP connection to this 127.0.0.1:38860, what would this be about?


Unable to push table from pgadmin to heroku

$
0
0

I have tried https://devcenter.heroku.com/articles/heroku-postgresql#local-setup but

heroku pg:psql

keeps returning “not found” heroku pg:psql app::databaseName returns The local psql command could not be located. I have add the path to the PostgreSQL bin file into my PATH but the error still appears.

We tried
heroku pg:psql push <database name> HEROKU_POSTGRESQL_BLUE_URL --app <app name>
but we ended up with Fatal password authentication failed for User.

We changed the pg_hba.conf file for the host method to be trust

# Type  Database  User    Address      Method  
# IPv4 local connections:
host    all       all     <address>    trust

But now the error returned is that the user doesn’t exist.

Nullable One To Many w/Join Table – Indexing

$
0
0

I have a question regarding database design, I am working with PostgreSQL using Hibernate for ORM, the design I inherited includes the following tables

users

user_id (pk)
user_name
user_dob …

sessions

session_id (pk)
session_date
session_duration …

user_sessions

user_id (fk)
session_id (fk)

As you can guess one user can have many sessions, the reason (I believe) the join table was used is because a session can be added before the user exists (and it is linked to the user after they signup), thus avoiding a nullable field in the sessions table…fine.

But I have recently been looking at creating some multi-column indexes to speed up certain queries and have realised that I cannot include the user in this index as the column is in a different table.

As I am fairly new to DB design I am just wondering if the above design is correct? Or am I in fact better off using a nullable FK in the session table to allow me to index the user relationship?

Schema for analytics table in Postgres

$
0
0

We use Postgres for analytics (star schema).
Every few seconds we get reports on ~500 metrics types.
The simplest schema would be:

timestamp      metric_type     value
78930890       FOO              80.9
78930890       ZOO              20

Our DBA has came up with a suggestion to flatten all reports of the same 5 seconds to:

timestamp   metric1     metric2     ...  metric500
78930890    90.9        20          ...  

Some developers push back on this saying this adds a huge complexity on development (batching data so it is written in one shot) and to maintainability (just looking at the table or adding fields is more complex).

Is the DBA model the standard practice in such systems or only a last resort once the original model is clearly not scalable enough?

EDIT: the end goal is to draw a line chart for the users. So queries will mostly be selecting a few metrics, folding them by hours/minutes, and selecting min/max/avg per hour (or any other time period).

EDIT: The DBA main argument is that reducing the number of rows x500 times will allow more efficient indexes and memory (the table will contain hundreds of millions of rows before this optimization). Then when selecting multiple metrics the suggested schema will allow one pass over the data instead of separate index search for each metric.

EDIT: 500 metrics is an “upper bound” but in practice most of the time only ~40 metrics are reported per 5 seconds (not the same 40 though)

Postgresql Trigger to insert records in my table from another one

$
0
0

I spent few days trying to resolve this question but it was impossible.

I have two tables:

Table1.

CREATE TABLE point
(
  id_point integer NOT NULL DEFAULT,
  layer text,
  elevation numeric,
  geom geometry(POINT,25830)
)

Table2.

CREATE TABLE point2
(
id_point2 integer NOT NULL DEFAULT,
layer text,
elevation numeric,
geom geometry (POINTZ, 25830)
)

In this situation I want get the values from point.elevation,point.geom and insert to point2.elevation, point2.geom each time I insert a new row in point table.

Also to say I need transform the point.geom value before to insert into point2.geom like this:

geom=st_setsrid((st_makepoint(st_x(point.geom),st_y(point.geom),point.elevation)), 25830)

I tried to create this trigger but I did not get the expected result, my trigger inserted all my records from point to point2, but I need do it for each new record from point table:

The function:

CREATE OR REPLACE FUNCTION function_prueba() RETURNS TRIGGER AS $$
DECLARE
BEGIN 
    INSERT INTO 
        point2(elevation,geom)
        VALUES
            NEW.point.elevation, 
            st_setsrid
                (
                    (
                           st_makepoint(st_x(NEW.point.geom),st_y(NEW.point.geom),NEW.point.elevation)
                    ), 25830
                )
                FROM
                         point
                ; 
    RETURN new;
END;
$$ language plpgsql;

The trigger:

CREATE TRIGGER trig_geotrans
     AFTER INSERT ON point
     FOR EACH ROW
     EXECUTE PROCEDURE function_prueba();

How can I resolve this question?
Thanks.

Copy few rows from one database to another

$
0
0

I have two PostgreSQL databases on two different machines and the network connection is slow.
I would like to copy few selected rows from the first instance to the second. Unfortunately, the full dump of single table is too big. A CLI solution would be fine.

Can not install Nominatim. How do I fix it? [Solved] [closed]

$
0
0

I download OSM map into postgresql.

It is all correct. Today I install Nominatim.

http://wiki.openstreetmap.org/wiki/Nominatim/Installation

I stop on the step: ./utils/setup.php –osm-file –all [--osm2pgsql-cache 18000]

~/src/Nominatim-2.1/utils/setup.php --osm-file /home/oleg/tmp/Russia/RU.osm.pbf --all --osm2pgsql-cache 12000

ERROR: DB Error: extension not found
DB Error: extension not found

I install DB (sudo pear install DB). I do not know where is error…

The answer

The problem is solved:

https://lists.openstreetmap.org/pipermail/geocoding/2013-October/001000.html

formatting SQL expressions in arcpy

$
0
0

I’m trying to use SelectLayerByAttribute to find the last point added to a FC. I figured I could use the objectid field because it’s serial. I wrote the following:

arcpy.SelectLayerByAttribute_management("controller", "NEW_SELECTION", """ "OBJECTID" = (SELECT MAX("OBJECTID") FROM controller) """) 

following the notes on found here

However, I get the following error:

Runtime error Traceback (most recent call last): File “”,
line 1, in File “c:program files
(x86)arcgisdesktop10.2arcpyarcpymanagement.py”, line 6494, in
SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid
expression Failed to execute (SelectLayerByAttribute).

I’m using a Postgres geodatabase and I know all the db’s require slightly different formatting requirements. This one I can’t figure out though.


How can you have a combined primary key for one table?

$
0
0

I have exactly the same question as posted here.
But in my case I do not use MySQL but Postgres. Can this be done with Postgres? All things indicate I can not use a combined index in Postgres, so assuming it can not: what solution would be most recommended?


Original question:

I have a table that contains information on invoices of a company. However, this company has two branches, and each of them has a unique invoicing sequence; a “Serie A” and “Serie B”, so to speak. However, this is one single company and I do not want to create two invoice tables. Rather, I somehow want to have two different auto-increments for one table. (…)

What I am doing right now is not using the primary key as invoice number (which would be ideal), but rather using a secondary column with the invoice id, which is incremented manually (well, using a PHP script, but it’s still not automatic), by checking the latest invoice for that particular series.

This is my current setup:

CREATE TABLE `invoices` (
  `id` mediumint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `invoicenumber` mediumint unsigned NOT NULL,
  `branch` enum('A','B') NOT NULL,
  `date` date NOT NULL,
  `client` varchar(100) NOT NULL
) COMMENT='' ENGINE='InnoDB';

To check the latest invoice, I run:

SELECT MAX(invoicenumber+1) AS new_invoice_number FROM invoices WHERE branch = 'A'

Postgres: What happens if your slave DB went down temporarily?

$
0
0

I have a master DB that is replicated to a slave DB through binary replication. What happens if this slave DB were to go offline for a period of time? Would it eventually catch up with the master as soon as it goes back online (without much intervention)?

Why aren't unsigned integer types available in the top database platforms?

$
0
0

Databases are usually very customizable with varying data types and custom lengths.

It surprises me, as I try to look for the syntax to use unsigned int types that they are not available from neither PostgreSQL and MS SQL Server. MySQL and Oracle seem to.

This seems like a glaring omission on their part – the next best perfomant option being a long/bigint, (8 byte integer), but could be completely unneccessary! Does anyone know why they would choose to not include native unsigned int support?

Stored procedure deadlocking itself

$
0
0

I have a strange situation, seen from the log:

Process 37278 waits for ExclusiveLock on advisory lock [16421,999999,12864385,2]; blocked by process 53807. 
Process 53807 waits for ExclusiveLock on advisory lock [16421,999999,12864395,2]; blocked by process 37278. 
Process 37278: SELECT * FROM zel_api.insert_event ($1 ,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24) 
Process 53807: SELECT * FROM zel_api.insert_event ($1 ,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24)",
"See server log for query details.",,,"SQL statement ""SELECT pg_advisory_xact_lock(999999, format('zel_event.%I', p_table_name)::regclass::oid::integer)""

This is already strange in itself, as it looks like two processes block the same advisory lock but neither can actually grab it.

The function which tries to acquire the lock is the following:

CREATE OR REPLACE FUNCTION zel_event.create_new_partition(
    p_table_name text
)
RETURNS void AS
$BODY$
DECLARE
    _schema text;
BEGIN
    IF NOT EXISTS (table from catalog)
    THEN
        PERFORM pg_advisory_xact_lock(999999, format('zel_event.%I', p_table_name)::regclass::oid::integer);

        IF NOT EXISTS (table from catalog)
        THEN
            EXECUTE format('
                CREATE TABLE %1$I.%2$I (
                    LIKE zel_event.%2$I INCLUDING ALL
                ) INHERITS (zel_event.%2$I)', _schema, p_table_name);
        END IF;
    END IF;
    RETURN;

END;
$BODY$
LANGUAGE plpgsql
SECURITY DEFINER;

To my eyes none of the ‘classic’ deadlock causes stands here as there is only one logic…
The idea behind it is to create the new table on demand where the demand can come at a really high frequency from several connections. It is called when an insertion function does the insert aimed at the parent table, being diverted by a dispatcher trigger to the appropriate child table.

This is how the transaction looks like:

INSERT INTO parent (zel_api.insert_event())
        |
     trigger
        |
        +----> child exists? ---no---> CREATE TABLE child
                      |                         |
                     yes                        |
                      |                         |
                      V                         V
              INSERT INTO child (zel_event.create_new_partition())   

The PostgreSQL version is 9.2

It would be very interesting to have some insights how this can happen (and mitigated, of course).

Is Postgres ignoring my function cost annotation?

$
0
0

Why does this:

create or replace function dummy() returns double precision as $$
SELECT random() $$
LANGUAGE SQL
COST 777;
explain select dummy();

return this:

Result  (cost=0.00..0.01 rows=1 width=0)

and not a cost of 777?

Simple PostgreSQL lookup table is inexplicably slow

$
0
0

I’m trying PostgreSQL 8.4.14 for storing triples, pieces of data of the form (String, String, String).
For speed, I’m not repeatedly storing strings but rather using two tables:

  • main table triples (subject BigInt, predicate BigInt, object BigInt)
  • lookup table entities (entityId BigInt, name Varying(40000))

I’ve added indexes and foreign keys on triples:

  • “nsubject_idx” hash (subject)
  • “npredicate_idx” btree (predicate)
  • “nobject_idx” hash (object)
  • “triples_subject_fkey” FOREIGN KEY (subject) REFERENCES entities(entityid)
  • “triples_predicate_fkey” FOREIGN KEY (predicate) REFERENCES entities(entityid)
  • “triples_object_fkey” FOREIGN KEY (object) REFERENCES entities(entityid)

and also indexes on entities:

  • “entities_pkey” PRIMARY KEY, btree (entityid) CLUSTER
  • “name_idx” hash (name)

Now it would be reasonable to assume that lookups are fast. They aren’t.

With 151M elements in triples and 44M in entities, the following query is immensely slow:

SELECT entityId FROM entities WHERE entityId in (SELECT object FROM triples LIMIT 10000);

It’s only 10.000 lookups, so I’d expect this to complete at high speed. Query plan:

 Nested Loop  (cost=288.69..3856.26 rows=43806140 width=8) (actual time=25.226..40110.699 rows=6959 loops=1)
   ->  HashAggregate  (cost=288.69..290.69 rows=200 width=8) (actual time=19.445..24.087 rows=6959 loops=1)
         ->  Limit  (cost=0.00..163.69 rows=10000 width=8) (actual time=0.013..15.792 rows=10000 loops=1)
               ->  Seq Scan on triples  (cost=0.00..2474009.68 rows=151135968 width=8) (actual time=0.012..14.101 rows=10000 loops=1)
   ->  Index Scan using entities_pkey on entities  (cost=0.00..17.82 rows=1 width=8) (actual time=5.756..5.759 rows=1 loops=6959)
         Index Cond: (entities.entityid = triples.object)
 Total runtime: 40112.383 ms

What would be happening here?

Note that this is even a trick query: because of the foreign key constraint, it is actually equivalent to SELECT object FROM triples LIMIT 10000.
For my use case, I’d need the actual lookup.

osm2pgsql: Import data in custom projection

$
0
0

I have osm-file and I want to import it to the postgresql via osm2pgsql with custom projection EPSG:20022. I look to the help and found out any keys like -E EPSG: but when I try to do with it I get the data in SRID=4326. Is there a way how to fix it?


Postgres sequence usage

$
0
0

I have an Postgres database with two users: dev and junit. Junit and dev objects are dropped and created regularly.

They both have the same objects unshared for tables, however, sequences are shared, and dev is the owner. I just use normal create statements.

I want to grant all to junit for the sequence but this fails with:

dev=> grant all on sequence serial to junit;
WARNING:  no privileges were granted for "serial"
GRANT

After this junit has no select rights on the sequence, how can I fix this?

I also would like to know whether there are recipes for this kind of setups, where I can use the same DDL for the two different users without modifying the script? I’ve been looking into schemas but I haven’t found the solution so far.

Calling another function inside a postgres function

$
0
0

I have a function A and function B.

Function A Name is f_a() and the code is:

Select id,address from A;

Function B Name is f_b() code is:

Select C.address,B.name 
From 
(Select id,name from B,
(Select * from f_a()) as C 
Where C.id = B.id;

However, when I execute the select * from f_b() the Error message says C.id does not exist. How can I run the function f_a() inside f_b().

Above function A and B are functions which returns table. I omitted the function head and end.

Import of OSM data into PostgreSQL using osm2pgsql: which formats are supported?

$
0
0

I followed a tutorial to install a PostgreSQL server on my Mac OS X Mavericks, and to import some OSM data with the osm2pgsql converter (https://www.mapbox.com/tilemill/docs/guides/osm-bright-mac-quickstart/).

I used OSM data in the .osm.pbf format for the import. That works fine.

Is it possible to use the formats .osm and .osm.bz2 as well? And maybe other formats?
Do I then need to add certain options to the osm2pgsql command, depending on the chosen format?

I intend to import only small amounts of data into the database, mainly for visualizing purposes in TileMill.

Missing libintl.8.dylib in shp2pgsql [closed]

$
0
0

I asked this on SO but since this is a GIS question I figured I should ask here too…

I’m trying to run shp2pgsql which I was able to do successfully in osx snow leopard 10.6.8 before I upgraded to osx Mavericks. I’m using Postgres 9.3 v2.1.0-2 with PostGIS 2.1.

The error I’m getting is:

dyld: Library not loaded: @loader_path/../lib/libintl.8.dylib
  Referenced from: /Library/PostgreSQL/9.3/bin/shp2pgsql
  Reason: image not found
Trace/BPT trap: 5

I’ve looked at
http://librelist.com/browser//homebrew/2013/5/15/missing-dylib-files/#85200742c00af0a239140b02f860d987 which suggests that brew install gettext. This has installed libintl.8.dylib at:

/usr/local/Cellar/gettext/0.18.3.1/lib/libintl.8.dylib

I have found some resources on how to make symbolic (?) links, but I’m unsure if this is the appropriate solution or even how to do it in this specific case since shp2pgsql is referencing:

@loader_path/../lib/libintl.8.dylib

How to export figure like Post In Action demonstrate?

$
0
0

I am reading the book “PostGIS In Action”

and now I follow the listing 2.4 here

SELECT AddGeometryColumn ('public','my_geometries',
'my_circular_strings',-1,'CIRCULARSTRING',2);
INSERT INTO my_geometries(name,my_circular_strings) 
VALUES ('Circle',
ST_GeomFromText('CIRCULARSTRING(0 0,2 0, 2 2, 0 2, 0 0)')), 
('Half Circle',
ST_GeomFromText('CIRCULARSTRING(2.5 2.5,4.5 2.5, 4.5 4.5)')), 
('Several Arcs',
ST_GeomFromText('CIRCULARSTRING(5 5,6 6,4 8, 7 9, 9.5 9.5,
11 12, 12 12)'));

And out put as

SELECT name,ST_NPoints(my_circular_strings) As cnpoints, 
ST_NPoints(ST_CurveToLine(my_circular_strings)) As lnpoints 
FROM my_geometries 
WHERE my_circular_strings IS NOT NULL;

and postgresql give me a table

                        name                        | cnpoints | lnpoints 
----------------------------------------------------+----------+----------
 Circle                                             |        5 |      129
 Half_Circle                                        |        3 |       65
 Several Arcs                                       |        7 |      113
(3 rows)

My question is the book also give me a figure as result like below

book's example

I want to make a picture like that ,how to do it??

Viewing all 1138 articles
Browse latest View live