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

How do I set the isolation level for SELECT statement?

$
0
0

Using PostgreSQL 9.5 I would like to make sure that when I run a SELECT statement I get consistent results, i.e. if the table is modified while the SELECT is running, I want to get the state that was valid when the SELECT started.

Basically, it’s like: Take a snapshot of a table, and don’t allow updates to the table while the snapshot is being taken, and then deliver the snapshot, while updates are allowed again.

If I understood isolation levels correctly, REPEATABLE READ is what I’m looking for, isn’t it?

Now my question is: How can I run a SELECT with REPEATABLE READ isolation level? Or am I missing something, and my approach is wrong? How would I solve this?


Mix Public Transportation data with pgRouting

$
0
0

I’m looking to mix 3rd party data, such as public transportation (routes, schedule is optional), with geographic data imported from shapefile/osm. With this I plan to give users an option to select a route taking into account public transportation.

How can I import public transportation data (GTFS) and use it with pgRouting routing algorithms, is this possible?

Given a lat/long how to find the nearest node in a route calculated by pgr_dijkstra?

$
0
0

I’m creating a table route_<route_id> for the route given by pgr_dijkstra, like this. In my application, I have a websocket with a mobile client that it’s sending it’s current position every x seconds. How can I find the nearest node from the table route_<route_id> and return a subset of the route, from the current position to the destination?

The SQL I’m using to generate the route table is:

CREATE TABLE route_route_id as SELECT seq, cost, 
             length::double precision, geom, 
             ST_AsGeoJSON(ST_LineMerge(geom))::json->'coordinates' as coordinates, 
             ST_AsGeoJSON(geom) 
             FROM pgr_dijkstra('
                SELECT gid AS id, source::integer, 
                       target::integer, length AS cost 
                           FROM "ways",
                               convert_to_lon_lat(lat, long, '"ways_vertices_pgr"'), 
                               convert_to_lon_lat(lat, long, '"ways_vertices_pgr"'), 
                               false, false) a LEFT JOIN ways b ON (a.id2 = b.gid);

how can i add tiled layer on Sharpmap using postgresql? [on hold]

$
0
0
map = new Map();
            try
            {
                //VectorLayer v = new VectorLayer("Point", new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "GhanaRegions", "Geometry", "id"));
                VectorLayer v = new VectorLayer("Point", new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "ghanalocation", "geometry", "id"));
                VectorLayer v2 = new VectorLayer("Point", new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "multilines", "geometry", "id"));
                v.Style.EnableOutline = true;
                v.Style.Outline = Pens.Black;
                v.Style.Fill = Brushes.Red;
                v.Style.PointColor = Brushes.LightGreen;
                SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Name");
                layLabel.DataSource = new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "citie33", "geometry", "id");
                layLabel.Enabled = true;
                layLabel.LabelColumn = "Name";
                layLabel.MaxVisible = 2;
                layLabel.MaxVisible = 190;
                layLabel.MinVisible = 130;
                layLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;
                layLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
                layLabel.PriorityColumn = "id";
                layLabel.Style.ForeColor = Color.Beige;
                layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
                layLabel.Style.BackColor = new System.Drawing.SolidBrush(Color.FromArgb(128, 255, 0, 0));
                layLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
                ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
                v.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
                v.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
                map.Layers.Add(v);
                map.Layers.Add(v2);

                mapImage1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(new BruTile.Web.OsmTileSource(), "OSM"));
                layLabel.Style.CollisionDetection = true;
                mapImage1.Map.ZoomToExtents();
                mapImage1.Refresh();
                mapImage1.ActiveTool = SharpMap.Forms.MapImage.Tools.Pan;
                mapImage1.Map = map;
                //ICoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();


            }
            catch (Exception ex)
            {
            }

org2ogr PostgreSQL to GPX

$
0
0

I already have found how to import GPX files into PostgreSQL using gdal/org2org. Now I am wondering of dumping into GPX files the results of my modification on dataset stored into PostgreSQL. But I have not found how to do so. Provided I have kept the original structure of tables generated by ogr2org, how can I dump their contents into a GPX file?

Deleting aged postgresql rows with foreignkey constraints

$
0
0

I have a postgresql table where I want to delete rows beyond a certain age. But the catch is that some foreign key constraints would be violated

For instance, if I run DELETE FROM links_reply WHERE id=3;, I get a violates foreign key constraint error.

But when I run DELETE FROM links_groupseen WHERE which_reply_id=3; and then run DELETE FROM links_reply WHERE id=3;, everything’s fine. Delete the child first, then the parent. Works!

Things get complicated though, when the respective tables are huge, and I’m trying to delete all rows before a certain date. E.g. I want to DELETE FROM links_reply WHERE "submitted_on" < now() - interval '30 days';. In this particular case, how do I take care of ‘the child table’ first? What command do I run?

Cannot create database PostgreSQL [on hold]

$
0
0

Working with Ubuntu on a server. I installed PostgreSQL.
Baiscally I am following this tutorial.

I am logged in with my user accoung max. I logged into the PostgreSQL account from the bash prompt like so

sudo -i -u postgres

Worked!
Then I started PostgreSQL by typing

psgl

Worked!

Then I created a test database like so

createdb test

Worked!

Then I created a new user like so

createuser max
> superuser -> yes!

Worked!

Then I wanted to create a database with a matching name to the newly created user, so

createdb max

Did not work, nor threw an error.

When I check my databases with list (logged in as postgres) I do not see the database max.
How would I create the database max for max that I can logg in from the bash (unter the account of max) with psql?

How to select number of affected rows to variable in function?

$
0
0
create or replace function test()
returns void as $$
begin
  update tbl set col1 = true where col2 = false;
  -- now I want to raise exception if update query affected more than 2 rows to rollback the update
end;
$$ language plpgsql;

How I can select number of affected rows to a variable in function?


How to configure sympa with plesk root access using mysql usename admin@localhost?

$
0
0

I’m trying to use MySql, but username is admin@localhost. It was root but plesk changes it to admin. I also tried PostgreSQL but after it asks for the root password there is another request to access database using its pss, but that also returns an error.

How can I rearrange this installation on plesk as user admin? Is there any extension to manage this installation like softaculos with PHP scripts?

Query bytea column using prefix

$
0
0

Assuming I have a table with a bytea column called data on a table called t, how can I:

  1. Write a query that returns all rows which have a specific prefix on their data column.
  2. Index the query.

I am using Postgresql 9.5.

Array of template type in PL/pgSQL function using %TYPE

$
0
0

I have a PostgreSQL database and a PL/pgSQL function that currently has arguments with copied types (for example IN arg_1 table_1.column_1%TYPE).

I want to update it so one of those arguments is actually an array of elements with that copied type. If it were a “normal” type, I’d just add [] after the type, like IN arg_1 integer[]. Following that logic, I tried adding the square brackets right after %TYPE, but I get a syntax error.

I couldn’t find anything about in the Postgres docs, nor did my google fu yield useful answers.
How can I achieve that?

Where to put snowball files to add a snowball stemmer to PostgreSQL database?

$
0
0

I’ve downloaded Czech snowball stemmer source files (.c, .h files) and compiled them into .o files and I know the correct SQL syntax for adding the dictionary (it’s in the docs).

I keep getting the following error though.

ERROR: no Snowball stemmer available for language "czech" and encoding "UTF8"

Where should I put the files in the PG installation for the CREATE TEXT SEARCH DICTIONARY to get them?

Postgresql uuid syntax error

$
0
0

I am trying to insert/update a foreign key field which is also a uuid to use the correct fk id for this set of data:

zuri=# ALTER TABLE newarts ALTER COLUMN jurisdiction_id SET DEFAULT a82857b6-e336-4c6c-8499-4...
ERROR:  syntax error at or near "c6c"
LINE 1: ...COLUMN jurisdiction_id SET DEFAULT a82857b6-e336-4c6c-8499-4...  
                                                             ^

This is just the latest iteration of this code, but it not the first time I have gotten this exact syntax error.

zuri=# UPDATE newarts set jurisdiction_id=a82857b6-e336-4c6c-8499-4...
ERROR:  syntax error at or near "c6c"
LINE 1: UPDATE newarts set jurisdiction_id=a82857b6-e336-4c6c-8499-4...
                                                          ^

I thought it might be an encoding issue, but that doesn’t seem to be it. The length is = 100, so that shouldn’t be it. The TYPE has already been set for uuid, so I don’t know what else to check. Thanks.

Postgres copy from CSV: how to format range variables?

$
0
0

Consider a Postgres table containing a range variable:

CREATE TABLE my_table (t_range tsrange, field1 text);

If I want to manually insert a row via psql, I can run:

INSERT INTO my_table (t_range, field1) VALUES ('[2014-06-02 07:00:00,infinity)'::tsrange, 'sometext');

Instead of inserting rows manually, I want to perform a bulk insert from a CSV file. I created a CSV file called my.csv using a similar format (using pipe as the delimiter to avoid having to escape the comma in the tsrange variable, and removing the explicit quote characters) with some content:

t_range|field1
'[2014-06-02 07:00:00,infinity)'::tsrange|'sometext1'
'[2014-06-02 07:00:00,infinity)'::tsrange|'sometext2'

Then from psql I ran

copy my_table FROM 'my.csv' DELIMITER '|' QUOTE '''' CSV HEADER;

but this failed with the error

ERROR:  malformed range literal: "[2014-06-02 07:00:00,infinity)::tsrange"
DETAIL:  Junk after right parenthesis or bracket.
CONTEXT:  COPY my_table, line 2, column t_range: "[2014-06-02 07:00:00,infinity)::tsrange"

How should I format a CSV file with range variables for use with copy?

Postgresql server is running, but service appears stopped

$
0
0

I am running Postgresql 9.4 as a service on Windows Server 2008 R2. It usually works as a charm, but now there is a situation when the server is apparently running (I can connect to it, Task Manager shows the running processes), but the Windows Service appears stopped.

I tried using pg_ctl, but I get:

$>pg_ctl restart -D c:psqldata94
pg_ctl: old server process (PID: 1436) seems to be gone
starting server anyway
server starting
2016-02-23 09:59:00 CET  LOG:  could not bind IPv6 socket: No error
2016-02-23 09:59:00 CET  HINT:  Is another postmaster already running on port  5432? If not, wait a few seconds and retry.
2016-02-23 09:59:00 CET  LOG:  could not bind IPv4 socket: No error
2016-02-23 09:59:00 CET  HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2016-02-23 09:59:00 CET  WARNING:  could not create listen socket for "*"
2016-02-23 09:59:00 CET  FATAL:  could not create any TCP/IP sockets

$>pg_ctl stop
pg_ctl: PID file "c:/psqldata94/postmaster.pid" does not exist
Is server running?

I tried looking for help online, but the only thing I could find, was this ominous post at psql-admin mailing list.

It’s exactly as the author of the post describes: the server is running, but it does not respond to commands. I suppose I can restart the underlying Windows server, but obviously this could have grave consequences if I just kill the running processes.

Does anybody know how to gracefully stop the server under this circumstances?


Selecting parent and child data

$
0
0

I have an application front end which speaks to a PostgreSQL database. I’m trying to find the most efficient way to extract a structure similar to the below.

CREATE TABLE people (
  person_id SERIAL PRIMARY KEY,
  fname     VARCHAR
);

CREATE TABLE items (
  person_id INTEGER REFERENCES people (person_id),
  item_id   SERIAL PRIMARY KEY,
  title     VARCHAR
);

INSERT INTO people (fname) VALUES
  ('Bob'),
  ('Jim'),
  ('Geoff');

INSERT INTO items (person_id, title) VALUES
  (1, 'Cat'),
  (1, 'Dog'),
  (1, 'Monkey'),
  (2, 'Elephant');

My current approach would be to list all of the items, then (within the application) iterate over each and run a SELECT. Something similar to this Pseudo code:

items = db.Query(SELECT * FROM people);
for item in items
   itemsub = db.Query(SELECT * FROM items WHERE id = item.person_id)

There are a number of reasons why I don’t like this.

  • It requires a minimum of 1 + N(People) queries to extract a simple data structure. For a page containing 1000 items, this would produce a ton of network traffic.
  • The application has to manually iterate over the list atleast twice more after the database, one to build the better structure and a second to render it.

  • It seems like something a database should be able to do

One other alternative would be to perform a JOIN, but this would result in extra processing to remove duplicates in the application

The resulting structure should be relatively simple:

peopleItems{ 
   array people{ 
             array items{}
         }
}

However, I can’t for the life of me work out the best approach.

Is there a way to automatically set triggers to update a field that is computed as an aggregation of other fields?

$
0
0

Let’s say that I have these tables: user, task, and submission. A user can create a submission which will be scored by the system, which will assign a value to submission.score.

Now, everytime a submission.score gets updated, I want to run an UPDATE query to update the user.total_score field, which is basically a SUM() over all submission.score pertaining to this user.

I know I can do this easily with a SQL trigger. However, sometimes the SUM() is not so simple. It may involve multiple fields over many different tables. Let’s say that I have not one, but many types of task: quiz_task, test_task, problem_task, and so on. Every type of task is different (and the user needs to do different things to solve them), and they have a different weight field. The score of a submission now needs to be factored by the weight of the task. I now need to add several triggers which all update the same user.total_score field (which I assume to be the sum of scores from all types of tasks) whenever a submission is re-evaluated (for any reason) or a task weight is changed.

My question is: is there a system, maybe a SQL construct (or something NoSQL?) that allows me to just declare a “computed field” and automatically add the needed triggers?

In theory, I think it’s doable: the system parses the query as a tree (storing intermediate values in the vertices) and everytime a vertex is updated, the system recomputes that subtree and “bubbles up”, updating all the intermediate values and (at the end) the “computed field”.

(In my user.total_score example, the tree would just be a root vertex connected to as many leaves as there are types of tasks)

In Postgres, how to preserve newline character in COPY TO command?

$
0
0

One of the column of my table contains string with ‘n’.

postgres=# select * from stringtest ;

 id |    inputstr

----+-----------------

  1 | Name : NOCr   +

    | r             +

    | Detail : Detail

(1 row)

But when I retrieve same table with COPY TO, new line character is getting escaped

postgres=# COPY (select * from stringtest) TO STDOUT WITH DELIMITER E't' ;

1       Name : NOCrnrnDetail : Detail

How can I make COPY TO command to replace ‘n’ with new line ??

Expected output :

postgres=# COPY (select * from stringtest) TO STDOUT WITH DELIMITER E't' ;

1       Name : NOCr
        r
        Detail : Detail

How to achieve this ??

Replicating redis foreign table via postgresql synchronous replication

$
0
0

I have synchronous replication between db server, DB1 and DB2. Tested, working.
I have a redis server (RD1), integrated with DB1 (master) via redis_fdw, no issues here. I was expecting foreign redis table to by synched between DB1 and DB2 after sync replication but it does not. I can see the table on DB2 (slave) when I type “d” in slave psql but when I want to select records, I get: “ERROR: could not access file “$libdir/redis_fdw”: No such file or directory”. The question is, how can I get redis updates from postgresql slave via postgresql sync. It is possible? I can integrate the slave DB with redis server but that would break the DB sync.
Thanks.

Accessing a column from PostgreSQL database and plotting it on Google Maps [closed]

$
0
0

I have a database created. This database has a column called Latitude/Longitude.
I wanna access this Latitude/Longitude field and plot it on Google Maps. What could be the best way to do this? I was thinking about using python.
Any suggestion would be appreciated. Thank you.

Viewing all 1138 articles
Browse latest View live