|
| 1 | +(cli)= |
| 2 | +(connect-cli)= |
| 3 | +# Using command-line programs with CrateDB |
| 4 | + |
| 5 | +This section provides a quick overview about a few CLI programs, and how to |
| 6 | +use them for connecting to CrateDB clusters. We recommend to use crash, |
| 7 | +psql, http ([HTTPie]), or curl. |
| 8 | + |
| 9 | +You can use them to quickly validate HTTP and PostgreSQL connectivity to your |
| 10 | +database cluster, or to conduct basic scripting. |
| 11 | + |
| 12 | +Before running the command-line snippets outlined below, please use the correct |
| 13 | +settings instead of the placeholder tokens `<hostname>`, `<username>` and |
| 14 | +`<password>`. |
| 15 | + |
| 16 | +When using CrateDB Cloud, `<hostname>` will be something like |
| 17 | +`<clustername>.{aks1,eks1}.region.{azure,aws}.cratedb.net`. |
| 18 | + |
| 19 | + |
| 20 | +(crash)= |
| 21 | +## crash |
| 22 | + |
| 23 | +```{div} |
| 24 | +:style: "float: right" |
| 25 | +{w=240px} |
| 26 | +``` |
| 27 | + |
| 28 | +The **CrateDB Shell** is an interactive command-line interface (CLI) tool for |
| 29 | +working with CrateDB. For more information, see the documentation about [crash]. |
| 30 | + |
| 31 | +```{div} |
| 32 | +:style: "clear: both" |
| 33 | +``` |
| 34 | + |
| 35 | +::::{tab-set} |
| 36 | + |
| 37 | +:::{tab-item} CrateDB and CrateDB Cloud |
| 38 | +:sync: server |
| 39 | + |
| 40 | +```{code-block} shell |
| 41 | +CRATEPW=<password> \ |
| 42 | + crash --hosts 'https://<hostname>:4200' --username '<username>' \ |
| 43 | + --command "SELECT 42.42;" |
| 44 | +``` |
| 45 | +::: |
| 46 | + |
| 47 | +:::{tab-item} CrateDB on localhost |
| 48 | +:sync: localhost |
| 49 | + |
| 50 | +```{code-block} shell |
| 51 | +# No authentication. |
| 52 | +crash --command "SELECT 42.42;" |
| 53 | + |
| 54 | +``` |
| 55 | +::: |
| 56 | + |
| 57 | +:::: |
| 58 | + |
| 59 | + |
| 60 | +(psql)= |
| 61 | +## psql |
| 62 | + |
| 63 | +```{div} |
| 64 | +:style: "float: right" |
| 65 | +{w=240px} |
| 66 | +``` |
| 67 | + |
| 68 | +**psql** is a terminal-based front-end to PostgreSQL. It enables you to type in |
| 69 | +queries interactively, issue them to PostgreSQL, and see the query results. |
| 70 | +For more information, see the documentation about [psql]. |
| 71 | + |
| 72 | +```{div} |
| 73 | +:style: "clear: both" |
| 74 | +``` |
| 75 | + |
| 76 | +::::{tab-set} |
| 77 | + |
| 78 | +:::{tab-item} CrateDB and CrateDB Cloud |
| 79 | +:sync: server |
| 80 | + |
| 81 | +```{code-block} shell |
| 82 | +PGUSER=<username> PGPASSWORD=<password> \ |
| 83 | + psql postgresql://<hostname>/crate --command "SELECT 42.42;" |
| 84 | +``` |
| 85 | +::: |
| 86 | + |
| 87 | +:::{tab-item} CrateDB on localhost |
| 88 | +:sync: localhost |
| 89 | + |
| 90 | +```{code-block} shell |
| 91 | +# No authentication. |
| 92 | +psql postgresql://crate@localhost:5432/crate --command "SELECT 42.42;" |
| 93 | +``` |
| 94 | +::: |
| 95 | + |
| 96 | +:::: |
| 97 | + |
| 98 | + |
| 99 | +(httpie)= |
| 100 | +## HTTPie |
| 101 | + |
| 102 | +```{div} |
| 103 | +:style: "float: right" |
| 104 | +{w=240px} |
| 105 | +``` |
| 106 | + |
| 107 | +The **HTTPie CLI** is a modern, user-friendly command-line HTTP client with |
| 108 | +JSON support, colors, sessions, downloads, plugins & more. |
| 109 | +For more information, see the documentation about [HTTPie]. |
| 110 | + |
| 111 | +```{div} |
| 112 | +:style: "clear: both" |
| 113 | +``` |
| 114 | + |
| 115 | +::::{tab-set} |
| 116 | + |
| 117 | +:::{tab-item} CrateDB and CrateDB Cloud |
| 118 | +:sync: server |
| 119 | + |
| 120 | +```{code-block} shell |
| 121 | +http "https://<username>:<password>@<hostname>:4200/_sql?pretty" \ |
| 122 | + stmt="SELECT 42.42;" |
| 123 | +``` |
| 124 | +::: |
| 125 | + |
| 126 | +:::{tab-item} CrateDB on localhost |
| 127 | +:sync: localhost |
| 128 | + |
| 129 | +```{code-block} shell |
| 130 | +http "localhost:4200/_sql?pretty" \ |
| 131 | + stmt="SELECT 42.42;" |
| 132 | +``` |
| 133 | +::: |
| 134 | + |
| 135 | +:::: |
| 136 | + |
| 137 | + |
| 138 | +(curl)= |
| 139 | +## curl |
| 140 | + |
| 141 | +```{div} |
| 142 | +:style: "float: right" |
| 143 | +{w=240px} |
| 144 | +``` |
| 145 | + |
| 146 | +The venerable **curl** is the ubiquitous command line tool and library for transferring |
| 147 | +data with URLs. For more information, see the documentation about [curl]. |
| 148 | + |
| 149 | +This example combines it with [jq], a lightweight and flexible command-line JSON processor. |
| 150 | + |
| 151 | +```{div} |
| 152 | +:style: "clear: both" |
| 153 | +``` |
| 154 | + |
| 155 | +::::{tab-set} |
| 156 | + |
| 157 | +:::{tab-item} CrateDB and CrateDB Cloud |
| 158 | +:sync: server |
| 159 | + |
| 160 | +```{code-block} shell |
| 161 | +echo '{"stmt": "SELECT 42.42;"}' \ |
| 162 | + | curl "https://<username>:<password>@<hostname>:4200/_sql?pretty" --silent --data @- | jq |
| 163 | +``` |
| 164 | +::: |
| 165 | + |
| 166 | +:::{tab-item} CrateDB on localhost |
| 167 | +:sync: localhost |
| 168 | + |
| 169 | +```{code-block} shell |
| 170 | +echo '{"stmt": "SELECT 42.42;"}' \ |
| 171 | + | curl "localhost:4200/_sql?pretty" --silent --data @- | jq |
| 172 | +``` |
| 173 | +::: |
| 174 | + |
| 175 | +:::: |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +[curl]: https://curl.se/ |
| 180 | +[crash]: inv:crate-crash:*:label#index |
| 181 | +[HTTPie]: https://httpie.io/ |
| 182 | +[jq]: https://jqlang.github.io/jq/ |
| 183 | +[psql]: https://www.postgresql.org/docs/current/app-psql.html |
0 commit comments