mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 00:58:02 +00:00
Add Fetch Data From An Endpoint In SQL as a Postgres TIL
This commit is contained in:
49
postgres/fetch-data-from-an-endpoint-in-sql.md
Normal file
49
postgres/fetch-data-from-an-endpoint-in-sql.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# Fetch Data From An Endpoint In SQL
|
||||
|
||||
The [`pgsql-http` extension](https://github.com/pramsey/pgsql-http) provides a
|
||||
variety of functions for allowing PostgreSQL to act as an HTTP client. This is
|
||||
a bit unorthodox and may not be a good idea in production systems. That said,
|
||||
it is cool that it is possible. Let's look at an example of it.
|
||||
|
||||
First, I've installed the extension on the Docker container running my local
|
||||
Postgres server.
|
||||
|
||||
```bash
|
||||
$ docker exec -it still-postgres-1 bash
|
||||
|
||||
$ apt-get update
|
||||
|
||||
$ apt-get install postgres-16-http # I'm running Postgres v16
|
||||
|
||||
$ exit
|
||||
```
|
||||
|
||||
Then I'll connect to a `psql` session in that container for the `postgres` database.
|
||||
|
||||
```bash
|
||||
$ docker exec still-postgres-1 psql -U postgres -d postgres
|
||||
```
|
||||
|
||||
Then I enable the extension.
|
||||
|
||||
```sql
|
||||
> create extension if not exists http;
|
||||
CREATE EXTENSION
|
||||
```
|
||||
|
||||
Now I can point a PostgreSQL statement at a live endpoint like
|
||||
[https://httpbun.com/ip](https://httpbun.com/ip) which will respond with a
|
||||
chunk of JSON including the IP address for that project's server. I do this
|
||||
using `http_get` which makes a `GET` request to the given endpoint. The body is
|
||||
included in the result set.
|
||||
|
||||
```bash
|
||||
> select content from http_get('http://httpbun.com/ip');
|
||||
content
|
||||
-----------------------------
|
||||
{ +
|
||||
"origin": "73.75.236.101"+
|
||||
} +
|
||||
|
||||
(1 row)
|
||||
```
|
||||
Reference in New Issue
Block a user