Saturday, May 26, 2012

Using HTTP to access postgres

On a whim I wanted to try making HTTP calls to access postgres so I slapped something together and called it pg_httpd. For the moment it can only query tables. For example, "http://localhost:54321/postgres/pg_stat_activity" will connect to the database named "postgres" and execute the SQL statement "SELECT * FROM pg_stat_activity;". It'll return the query output as JSON.

An example using curl:
curl http://localhost:54321/postgres/pg_stat_activity
The resulting JSON has been reformatted for easier viewing:
[
   {
      "datid":"12774",
      "datname":"postgres",
      "procpid":28887,
      "usesysid":"10",
      "usename":"markwkm",
      "application_name":"",
      "client_addr":null,
      "client_hostname":null,
      "client_port":-1,
      "backend_start":"2012-05-26 14:11:41.91862-07",
      "xact_start":"2012-05-26 14:11:41.920169-07",
      "query_start":"2012-05-26 14:11:41.920169-07",
      "waiting":false,
      "current_query":"SELECT * FROM pg_stat_activity;"
   }
]

Labels: ,