Skip to content

Commit bbc13cd

Browse files
authored
Programatically accessing Heroku PostgreSQL from GitHub Actions
1 parent ca76798 commit bbc13cd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Programatically accessing Heroku PostgreSQL from GitHub Actions
2+
3+
My [db-to-sqlite](https://github.com/simonw/db-to-sqlite) tool can connect to a PostgreSQL database, export all of the content and write it to a SQLite database file on disk.
4+
5+
I wanted to use it in a GitHub Action - but that meant I needed code running in the action workflow to be able to access my Heroku PostgreSQL database directly.
6+
7+
It turns out the `DATABASE_URL` environment variable in Heroku has everything you need to connect to that database from elsewhere.
8+
9+
If you have the `heroku` CLU tool installed and authenticated the following one-liner does the job:
10+
11+
db-to-sqlite $(heroku config:get DATABASE_URL -a simonwillisonblog) simonwillisonblog.db
12+
13+
To configure `heroku` in a GitHub Action you need to set a `HEROKU_API_KEY` environment variable.
14+
15+
You can create an OAuth token on your laptop like this:
16+
17+
% heroku authorizations:create --scope=read-protected
18+
Creating OAuth Authorization... done
19+
Client: <none>
20+
ID: 4dd42e6c-5c89-4389-a9b1-4a5388b88517
21+
Description: Long-lived user authorization
22+
Scope: read-protected
23+
Token: xxx copy and paste this bit xxx
24+
25+
Then you can paste the token into a GitHub repository secret called `HEROKU_API_KEY`.
26+
27+
Here's a fragment of my action workflow that creates the SQLite database pulling data from Heroku:
28+
29+
```yaml
30+
- name: Import Heroku DB into SQLite
31+
env:
32+
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
33+
run: |-
34+
db-to-sqlite $(heroku config:get DATABASE_URL -a simonwillisonblog) simonwillisonblog.db \
35+
--table auth_permission \
36+
--table auth_user \
37+
--table blog_blogmark \
38+
--table blog_blogmark_tags \
39+
--table blog_entry \
40+
--table blog_entry_tags \
41+
--table blog_quotation \
42+
--table blog_quotation_tags \
43+
--table blog_tag \
44+
--table django_content_type \
45+
--table redirects_redirect
46+
```

0 commit comments

Comments
 (0)