Skip to content

Instantly share code, notes, and snippets.

@mkscrg
Created August 5, 2016 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkscrg/6cb5e56be17ddf68cea92b80e9c318b2 to your computer and use it in GitHub Desktop.
Save mkscrg/6cb5e56be17ddf68cea92b80e9c318b2 to your computer and use it in GitHub Desktop.

Slack logs to Postgres

Export the Slack team's public logs. You'll get a directory with a users.json file and a directory for each channel e.g. wheelhouse/.

Use jq to pull the JSON objects out of the root array:

cat users.json | jq -c '.[]'

Use sed to kinda sorta CSV escape the JSON objects:

... | sed 's/"/""/g' | sed 's/^/"/' | sed 's/$/"/'

Use psql to insert these into a table:

... | psql CONN_STR -c '
  drop table if exists users;
  create table users (json_data jsonb);
  copy users from stdin with (format csv);
  '

All together now:

cat users.json |
  jq -c '.[]' |
  sed 's/"/""/g' | sed 's/^/"/' | sed 's/$/"/' |
  psql CONN_STR -c '
    drop table if exists users;
    create table users (json_data jsonb);
    copy users from stdin with (format csv);
    '

Or for all the messages in a channel:

cat wheelhouse/* |
  jq -c '.[]' |
  sed 's/"/""/g' | sed 's/^/"/' | sed 's/$/"/' |
  psql CONN_STR -c '
    drop table if exists wheelhouse;
    create table wheelhouse (json_data jsonb);
    copy wheelhouse from stdin with (format csv);
    '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment