Environment variables in Postgres C extension



  • I cannot get environment variable in my PostgreSQL C extension code.

    For example, this function always returns 111:

    #include "postgres.h"
    #include "fmgr.h"
    #include 
    

    PG_MODULE_MAGIC;

    PG_FUNCTION_INFO_V1(myinc);
    Datum
    myinc(PG_FUNCTION_ARGS)
    {
    int32 arg = PG_GETARG_INT32(0);
    char* envar = getenv("MYINC");
    if (envar) {
    PG_RETURN_INT32(arg + atoi(envar));
    } else {
    PG_RETURN_INT32(111);
    }
    }

    While this C program works as expected, it prints whatever MYINC is:

    #include 
    #include 
    

    int main()
    {
    printf("MYINC: %s", getenv("MYINC"));
    return 0;
    }



  • That should work just fine. Note, however, that it will take the variable from the environment of the PostgreSQL server process, not your current client process.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2