Incorrect syntax near 'PROCEDURE' postgresql - postgresql

My database is hosted in Amazon and I am using pgAdmin 4 to connect to it.
I copy-pasted snippet from https://www.postgresql.org/docs/11/sql-createprocedure.html
CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
$$;
The issue is that I get 'incorrect syntax near 'PROCEDURE' ' error
What is done wrong? Not sure how I check version of postgresql itself

With Postgres 10, you need to use a function:
CREATE function insert_data(a integer, b integer)
returns void
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a), (b);
$$;

According to PostgreSQL documentation, syntax is supported in versions 11 and 12.
PostgreSQL: Documentation: 11: CREATE PROCEDURE
Documentation → PostgreSQL 11
Supported Versions: Current (11)
Development Versions: 12 / devel
Check PostgreSQL version on your server, run this query from PgAdmin:
SELECT version();

Related

Get session number from namespace USERENV in postgresql

While migrating oracle to postgresql I came up with the query:
Oracle:
select SYS_CONTEXT('USERENV','SID') from dual;
What will be the equivalent in PostgreSQL?
The only thing I can think of is the backend PID
select pg_backend_pid();

What is PostgreSQL equivalent for MSSQL msdb.dbo.sp_start_job?

What is PostgreSQL equivalent for MSSQL EXEC msdb.dbo.sp_start_job 'jobName'? Does anyone know is there such a procedure in Postgres?

Do I have to create extension on every connect to database?

I want to use uuid_generate_v4 in PostgreSQL. I am currently using version 11, but plan to upgrade to 12.
When I try to call the function I get this error after every connect:
test=# SELECT uuid_generate_V4();
ERROR: function uuid_generate_v4() does not exist
LINE 1: SELECT uuid_generate_V4();
And I have to rerun:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Minimum "working" example
CREATE DATABASE test;
\connect test;
SELECT uuid_generate_V4(); -- Fails makes sense
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_V4(); -- Works
\connect postgres --- Close connection to test
\connect test;
SELECT uuid_generate_V4(); -- Fails makes no sense
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_V4(); -- Works
Do I really need to add/create the extension at every connect or am I doing something wrong?
I have the same problem with other extensions as well, but uuid is the most crucial to get working.

ERROR: function addgeometrycolumn(unknown..), when inserting sql file in to postgis database

I am trying to import the spatial file in to my database
Firstly i have created a database using postgis template as below
createdb -T template_postgis database_name;
I have postgis installed already on my machine
POSTGIS="2.1.1 r12113" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.9.0, released 2011/12/29" LIBXML="2.7.8" LIBJSON="UNKNOWN" TOPOLOGY RASTER
Postgres version : psql (9.3.2, server 9.1.11)
Secondly i have converted the spatial file in to sql file as below
shp2pgsql -s 3425 Aspire.shp test_for_shape_data database_name > shapefile_data.sql
And now i am trying to import this sql file(shapefile_data.sql) in to my database(database_name) like below
psql -d database_name username -f shapefile_data.sql
But i am getting the following error
user#user:~/user/spice$ psql -d psql -d database_name username -f shapefile_data.sql
SET
SET
BEGIN
psql:shapefile_data.sql:30: NOTICE: CREATE TABLE will create implicit sequence "test_for_shape_data_gid_seq" for serial column "test_for_shape_data.gid"
CREATE TABLE
psql:shapefile_data.sql:31: NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_for_shape_data_pkey" for table "test_for_shape_data"
ALTER TABLE
psql:shapefile_data.sql:32: ERROR: function addgeometrycolumn(unknown, unknown, unknown, unknown, unknown, integer) is not unique
LINE 1: SELECT AddGeometryColumn('','test_for_shape_data','geom','42...
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.
psql:shapefile_data.sql:33: ERROR: current transaction is aborted, commands ignored until end of transaction block
psql:shapefile_data.sql:34: ERROR: current transaction is aborted, commands ignored until end of transaction block
...........
.......
Why it is telling me that i don't have AddGeometryColumn function even though i have created the database with postgis template ?
So how to avoid this ?
Also when i try to manually enable the postgis functions its telling that already exists
database_name=# CREATE EXTENSION postgis;
ERROR: type "spheroid" already exists
database_name=# CREATE EXTENSION postgis_topology;
ERROR: required extension "postgis" is not installed
So how to clear this error and insert the shapefile sql file in to database ?
It sounds like you've got an old PostGIS install that was created from an SQL script, pre-extension support. It probably only has some of the functionality and features you expect. This sort of behaviour can occur when you've got a PostGIS 1.5 schema and a PostGIS 2.0 install, etc.
Try creating your DB from template0 instead, and running CREATE EXTENSION postgis; then doing a restore. I suspect your template_postgis contains an old version of the extension schema.
See the PostGIS upgrade guide.

NOTICE: using pg_pltemplate information instead of CREATE LANGUAGE parameters

I am getting this error message (shown in title), when I run this SQL statement in PG 8.4:
psql -h localhost -U postgres -d mydb -c "CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler VALIDATOR plpgsql_validator"
What's causing this (it used to work with earlier versions of PG), and how do I resolve it?
This procedure has been simplified. For plpgsql simply use:
CREATE LANGUAGE plpgsql;
The error message tells you, that plpgsql is among a number of predefined languages listed in the system table pg_pltemplate. Just have a look:
SELECT * FROM pg_pltemplate;
In PostgreSQL 9.0 and later, PL/pgSQL is pre-installed by default.