Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:34:26+00:00 2026-05-26T16:34:26+00:00

Hello I am just learning postGIS and thus postgresql (9.1) and am trying to

  • 0

Hello I am just learning postGIS and thus postgresql (9.1) and am trying to save some time copying the same code over and over by creating an sql function to reproject some spatial data.

Create Function reproject_shapefile(text,text,numeric) returns void as $$

    -- Reprojects shapefiles given that they follow the pattern "gid * the_geom"

    CREATE TABLE $2 AS
        SELECT *, ST_Transform(the_geom,$3) AS the_geom2
        FROM $1;
    Alter table $2 add Primary Key (gid);
    Alter table $2 drop column the_geom;
    Alter table $2 rename column the_geom2 to the_geom;
$$ Language SQL;

I read over the docs specifying how to do this, but everytime I try to create the function from the sql editor in pgAdmin, I receive the following error:

ERROR:  syntax error at or near "$2"
LINE 5:     CREATE TABLE $2 AS
                     ^

********** Error **********

ERROR: syntax error at or near "$2"
SQL state: 42601
Character: 175

Unlike the error messages in python, this tells me absolutely nothing of use, so I am hoping that someone can point me in the right direction on how to fix this error.

If there is some way to perform this same function using python feel free to post that as a solution instead/ as well, as python syntax is much easier for me to understand than ancient SQL.

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:34:27+00:00Added an answer on May 26, 2026 at 4:34 pm

    You can not write dynamic SQL in this form. Parameters can only pass in values, not identifiers. Something like this is impossible in an SQL function:

    CREATE TABLE $2 AS
    

    You need to write a plpgsql function for that and use EXECUTE. Could look like this:

    CREATE OR REPLACE FUNCTION reproject_shapefile(text, text, numeric)
      RETURNS void as $$
    BEGIN
    
    EXECUTE '
       CREATE TABLE ' || quote_ident($2) || ' AS
       SELECT *, ST_Transform(the_geom,$1) AS the_geom2
       FROM  ' || quote_ident($1)
    USING $3;
    
    EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' ADD PRIMARY KEY (gid)';
    EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' DROP COLUMN the_geom';
    EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' RENAME column the_geom2 TO the_geom';
    
    END;
    $$ Language plpgsql;
    

    Major points

    • plpgsql function, not sql
    • EXECUTE any query with dynamic identifiers
    • use quote_ident to safeguard against SQLi
    • pass in values with the USING clause to avoid casting and quoting madness.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning scala, and I wrote the hello,world program like this: object HelloWorld
I am just learning some pointers stuff in C and I happened to learn
I have just started learning Javascript. I want Hello World! to be written to
Hello I am just learning more about using classes in PHP. I know the
Hello everyone I am just learning my first programming language and have just corrected
So I'm just learning some new stuff in C# & Python. Turns out both
I've just begun learning some x86 assembly on win32, and I've used masm with
I am just learning JS cookies. I am able to save the cookie when
I'm just learning Django and trying to setup the View and URLconfs ( http://djangobook.com/en/2.0/chapter03/
i just started learning MFC..found a tutorial here http://bit.ly/j2uhHO ..just tried the same thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.