1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-17 22:18:01 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud
a05840633f Add Run SQL Against Remote Postgres Database as a Heroku TIL 2024-03-07 17:25:29 -06:00
jbranchaud
22fde22447 Add Get A Slice Of The Ends Of An Array as a jq TIL 2024-03-07 10:59:20 -06:00
3 changed files with 67 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1390 TILs and counting..._
_1392 TILs and counting..._
---
@@ -387,6 +387,7 @@ _1390 TILs and counting..._
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
- [Diagnose Problems In A Heroku Postgres Database](heroku/diagnose-problems-in-a-heroku-postgres-database.md)
- [Run SQL Against Remote Postgres Database](heroku/run-sql-against-remote-postgres-database.md)
- [Set And Show Heroku Env Variables](heroku/set-and-show-heroku-env-variables.md)
- [SSH Into Heroku Server Hosting App](heroku/ssh-into-heroku-server-hosting-app.md)
@@ -528,6 +529,7 @@ _1390 TILs and counting..._
- [Filter Out Results Based On List Of Values](jq/filter-out-results-based-on-list-of-values.md)
- [Find All Objects In An Array Where Key Is Set](jq/find-all-objects-in-an-array-where-key-is-set.md)
- [Find All Objects With A Matching Key Value Pair](jq/find-all-objects-with-a-matching-key-value-pair.md)
- [Get A Slice Of The Ends Of An Array](jq/get-a-slice-of-the-ends-of-an-array.md)
- [Get The First Item For Every Top-Level Key](jq/get-the-first-item-for-every-top-level-key.md)
- [Get The Last Item From An Array](jq/get-the-last-item-from-an-array.md)
- [Reduce Object To Just Entries Of A Specific Type](jq/reduce-object-to-just-entries-of-a-specific-type.md)

View File

@@ -0,0 +1,27 @@
# Run SQL Against Remote Postgres Database
You can access a remote Heroku PostgreSQL database through a `psql` session
using the following command:
```bash
heroku pg:psql --app my-app
```
That opens an interactive psql session.
If instead you'd like to run a single SQL script against that remote database,
you can redirect that script to the connection.
```bash
heroku pg:psql --app my-app < query.sql
```
The results of running that SQL will be written to stdout.
You can take this a step further by redirecting the output into another file so
that you can review and search the results at your convenience, rather than
only having them appear in your terminal.
```bash
heroku pg:psql --app my-app < query.sql > results.out
```

View File

@@ -0,0 +1,37 @@
# Get A Slice Of The Ends Of An Array
[`jq`](https://jqlang.github.io/jq/) has an array slice syntax that allows us
to grab a subset of values from an array. The general form of this syntax is
`[n:m]` where `n` is the index of the start of our slice (inclusive) and `m` is
the index of the end of our slice (non-inclusive).
If we omit the `n`, it defaults to `0`, or the start of the array. Similarly,
if we omit the `m`, it defaults to the end of the list.
Knowing that, we can grab the first few elements of the list like so:
```bash
echo '["a","b","c","d","e","f","g"]' | jq '.[:3]'
[
"a",
"b",
"c"
]
```
We can also use a negative index value to count back from the end of the array.
This allows us to grab a slice from some point relative to the end of the list.
Instead of having to compute it based on knowing the length of the array.
Knowing that, we can grab the last few elements of the list like so:
```bash
echo '["a","b","c","d","e","f","g"]' | jq '.[-3:]'
[
"e",
"f",
"g"
]
```
[source](https://jqlang.github.io/jq/manual/#array-string-slice)