ERROR: could not access file "$libdir/postgis-2.0" postgresapp - sql

I am running postgresapp 9.2.4.3 and postgis and trying to add a geometry column to a table. The postgis extension is running.
When I run:
SELECT AddGeometryColumn('public'::varchar,'gloutline'::varchar,'geom'::varchar,'4326','MULTIPOLY ON','2');
I'm getting the following errors:
ERROR: could not access file "$libdir/postgis-2.0": No such file or directory
LINE 1: ALTER TABLE public.gloutline ADD COLUMN geom geometry(MultiP...
QUERY: ALTER TABLE public.gloutline ADD COLUMN geom geometry(MultiPolygon, 4326)
CONTEXT: PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,character varying,integer,character varying,integer,boolean) line 110 at EXECUTE statement
SQL statement "SELECT AddGeometryColumn('',$1,$2,$3,$4,$5,$6,$7)" PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,integer,character varying,integer,boolean) line 5 at SQL statement

A reinstall of postgress.app including the application support files fixed the problem.

Related

Keep getting error when trying to use '\copy' in sql

Here is the query I've been trying to run:
create or replace function save_to_csv(mesa text)
returns void as $$
begin
execute '\copy (select distinct aid::text,
rid::text,
did::text
from ' || mesa ||
')
to ''/home/datascientist/Data/User_Journey_Map/updated_data/all_ids/'|| mesa ||'_ids.csv''';
end;
$$
language 'plpgsql';
I keep getting this error:
SQL Error [42601]: ERROR: syntax error at or near "\"
Where: PL/pgSQL function save_to_csv(text) line 3 at EXECUTE
SQL statement "SELECT save_to_csv(mesa)"
PL/pgSQL function save_tables() line 21 at PERFORM
However, if I delete the ' \' before 'copy' I get this error:
SQL Error [58P01]: ERROR: could not open file "/home/datascientist/Data/User Journey Map/updated_data/all_ids/tca__clicked_ids.csv" for writing: No such file or directory
Hint: COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy.
Where: SQL statement "copy (select distinct aid::text, rid::text, did::text from tca__clicked) to '/home/datascientist/Data/User Journey Map/updated_data/all_ids/tca__clicked_ids.csv'"
PL/pgSQL function save_to_csv(text) line 3 at EXECUTE
SQL statement "SELECT save_to_csv(mesa)"
PL/pgSQL function save_tables() line 21 at PERFORM
I've tried implementing the solutions given at:
PostgreSQL syntax error when using EXECUTE in Function
and
I am trying to copy a file, but getting error message , but neither have helped and I can't find anything else that looks helpful.

PostgreSQL accessing nested uppercased properties

I've recently switched from mongoDB to postgreSQL and for a while I got stuck with this problem - I can't seem to find a way to access nested property of an object. Yes, I shouldn't be having uppercased column/table names to start with, but I really want to keep naming consistency.
Let's say I have the following db table:
users {
ID: bigint
}
Now let's say I want to get deleted user(old) ID, how do I access this ID?
create or replace function deleted()
returns trigger AS
$body$
begin
perform pg_notify('deleted', ----->WHAT GOES HERE<-----);
return new;
end;
$body$
I've tried filling in placeholder with old."ID", then I get following error:
ERROR: function pg_notify(unknown, bigint) does not exist
LINE 1: SELECT pg_notify('deleted', old."ID")
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT pg_notify('deleted', old."ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42883
If I try doing this -> old.ID then I get
ERROR: record "old" has no field "id"
CONTEXT: SQL statement "SELECT pg_notify('deleted', old.ID)"
PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Also I've tried this: `"old.ID", then I get the following:
ERROR: column "old.ID" does not exist
LINE 1: SELECT pg_notify('deleted', "old.ID")
^
QUERY: SELECT pg_notify('deleted', "old.ID")
CONTEXT: PL/pgSQL function deleted() line 3 at PERFORM
SQL state: 42703
Thank you for your patience.
pg_notify() wants two arguments of text datatype. So you can cast your bigint argument to that datatype:
pg_notify('deleted', (old."ID")::text)

Postgresql 11 - Create Procedure to Execute COPY function

I'm currently trying to create a procedure to automatically copy data into my database when I call the procedure. However, every time I call it I get the following error:
ERROR: column "name" does not exist
LINE 1: SELECT format('COPY test(%L) FROM %s CSV HEADER', name, '/Us...
How does the column not exist? Here's everything I've written out:
CREATE PROCEDURE
test_insert() AS
$$
BEGIN
EXECUTE format('COPY test(%L) FROM %s CSV HEADER', name, '/Users/Receiving.csv');
END;
$$ LANGUAGE plpgsql;
If you use name without single quotes, it is interpreted as a column name in the (tacit) SELECT statement
SELECT format('...', name, '...')
that PL/pgSQL runs when you execute your function.
Since this SELECT statement does not have a FROM clause, you get the observed error.
The solution is to use a string literal instead, i.e. write 'name' instead of 'name'.

Execution of a query in a function plpgsql (POSTGIS)

I'm looking for all the points that are inside a polygon. The query works for me, but when I add them to a function plggsql I get an error.
Query Ok
SELECT id, the_geom
FROM vialidad
WHERE ST_Contains('POLYGON((-99.1981315612793 19.357945,-99.1981315612793 19.376003,-99.161634 19.376003,-99.161634 19.357945,-99.1981315612793 19.357945))', ST_AsText(the_geom));
When I add the query to the function plpgsql and the polygon that was created is in polygon variable.
EXECUTE 'SELECT id,the_geom FROM vialidad WHERE ST_Contains('||polygon||',ST_AsText(vialidad_cdmx_vertices_pgr.the_geom));'
INTO nodes
USING polygon;
Console Error
QUERY: SELECT id, the_geom FROM vialidad WHERE ST_Contains(POLYGON((-99.1981315612793 19.357945,-99.1981315612793 19.376003,-99.161634 19.376003,-99.161634 19.357945,-99.1981315612793 19.357945)),ST_AsText(the_geom));
CONTEXT: PL/pgSQL function get_nodes_between_two_lines(integer,integer) line 37 at EXECUTE
********** Error **********
ERROR: syntax error at or near "19.357945"
SQL state: 42601
Many thanks in advance for any possible help.
Do not concatenate strings into your SQL query. You are already passing the variable to the execute command with the using keyword, but your query doesn't use a place holder.
EXECUTE 'SELECT id,the_geom FROM vialidad WHERE ST_Contains($1,ST_AsText(vialidad_cdmx_vertices_pgr.the_geom))'
-- ^ here
INTO nodes
USING polygon;
The fact that you are selecting two columns but only supply a single target variable with the INTO clause is highly suspicious.
But I don't think you need dynamic SQL at all, assuming polygon is a variable, the following should work just fine:
SELECT id, the_geom
into id_var, geom_var
FROM vialidad
WHERE ST_Contains(polygon,ST_AsText(vialidad_cdmx_vertices_pgr.the_geom))
Note that if you have a column named polygon in the table vialidad this is going to give you problems. You should rename your variable polygon then to have a different name. Many people simply put e.g. a l_ in front of the variable names, l_polygon or a p_ for parameters.
Seems like when run query separately, there is single quote with the first argument of st_contains
ST_Contains('POLYGON((-99.1981315612793 19.357945,-99.1981315612793 19.376003,-99.161634 19.376003,-99.161634 19.357945,-99.1981315612793 19.357945))', ST_AsText(the_geom));
But in console error there are no single quote around the polygon. You can try function with single quote inserted as
EXECUTE 'SELECT id,the_geom FROM vialidad WHERE ST_Contains('''||polygon||''',ST_AsText(vialidad_cdmx_vertices_pgr.the_geom));'
INTO nodes
USING polygon;

CREATE SCHEMA using a name stored

I am trying to create a new schema on my postgres database which name is stored on an existing table, my query look like this:
CREATE SCHEMA (SELECT name FROM table)
But I am getting a syntax error. What am I doing wrong? Is this a valid way to create the new schema? Which other solution exist for this issue?
You can do it with dynamic sql:
do
$$
declare s_name text;
begin
-- make sure there is exactly one row in that table!
-- otherwise you need some where condition or an aggregat to ensure that.
SELECT name INTO s_name FROM some_table;
execute 'create schema '|| quote_ident(s_name);
end;
$$
It is not possible, create schema syntax required a valid schema name (i.e. valid schema name string). in your case it will throw exception as
********** Error **********
ERROR: syntax error at or near "("
SQL state: 42601
Character: 15