| name | bq-cli |
| description | BigQuery via the standalone `bq` CLI (plain `bq ...`, NOT `gcloud bq`). Queries, load/extract jobs, datasets, tables, views, streaming inserts, job control, and table/view IAM. |
bq — BigQuery command-line tool (standalone, not gcloud)
Overview
bq is the Python-based, general-purpose BigQuery command-line tool bundled with the Google Cloud CLI (and preinstalled in Cloud Shell). It is a standalone tool invoked as plain bq ... — e.g. bq query, bq load, bq mk — not gcloud bq .... It covers the day-to-day BigQuery surface: running queries, loading and extracting data, creating/updating/deleting datasets, tables, views and routines, inspecting jobs, streaming inserts, IAM policies, reservations, connections, and Data Transfer Service configs.
Do not confuse with gcloud bq. The GA gcloud bq command group covers only BigQuery Migration Service workflows (gcloud bq migration-workflows — 4 commands). Everything else BigQuery on the CLI is this standalone bq tool. See ../bq/SKILL.md for the gcloud bq surface.
Command format: bq [--global_flags] COMMAND [--command_flags] [ARGUMENTS] — global flags go before the command name, command-specific flags after it. bq respects the active gcloud credentials and, by default, the gcloud config (--use_gcloud_config=true), so gcloud config set project / gcloud auth login (or service-account impersonation via gcloud config set auth/impersonate_service_account) carry over.
Global flags
Usable with any command, where applicable (full list: bq --help):
| Flag | Value | Default | Description |
|---|
--project_id | PROJECT | gcloud config | Default project to use for requests. |
--location | LOCATION | | Region or multi-region (e.g. US, EU, us-central1). Required for bq cancel and bq show -j; optional for query, cp, load, extract, partition, update, wait, and for mk/ls with reservation/commitment/dataset flags. Ignored by other commands. |
--dataset_id | PROJECT:DATASET or DATASET | | Default dataset for requests, so table args can be bare table names. Ignored when not applicable. |
--format | none|json|prettyjson|csv|sparse|pretty | per command | Output format. pretty (formatted table), sparse (simpler table), and prettyjson (readable JSON) are human-oriented; json (compact JSON) and csv are for passing to other programs. If unset, a format is chosen per command. |
-q, --quiet | boolean | false | Suppress status updates while jobs run — use in scripts. |
--apilog | file path, stdout, stderr | off | Log all API requests/responses to a file or stream (useful for debugging; - or stdout prints to console). |
--job_id | JOB_ID | generated | Explicit job ID for job-launching commands (cp, extract, load, query). Use --fingerprint_job_id=true instead to derive the ID from the job config (prevents accidental duplicate runs). |
-sync, --synchronous_mode | boolean | true | If true, wait for command completion and use the job completion status as the exit code. If false (--nosync), return immediately after job creation and use the success of job creation as the exit code. |
--headless | boolean | false | Run without user interaction: never break into a debugger, less informational printing. Set in automated environments. |
--bigqueryrc | PATH | ~/.bigqueryrc | Path to the config file with default flag values. Resolution order: --bigqueryrc flag, BIGQUERYRC env var, ~/.bigqueryrc. |
--api | ENDPOINT | https://bigquery.googleapis.com | API endpoint to call. |
--job_property | KEY:VALUE | | Extra key-value pair for the job configuration properties field (repeatable). |
--debug_mode | boolean | false | Show tracebacks on Python exceptions. |
--httplib2_debuglevel | integer | | Log HTTP requests/responses to stderr when > 0. |
Resource naming
bq arguments use PROJECT:DATASET.TABLE — colon between project and dataset, period between dataset and table (e.g. myProject:myDataset.myTable). Omit the project to use the default project (myDataset.myTable). Inside GoogleSQL query text, use periods everywhere: `myProject.myDataset.myTable`. Quote identifiers with backticks if they contain characters other than letters/digits/underscores or reserved keywords. A trailing : or . on an identifier passed to ls marks it as a project or dataset. The equals sign in --flag=value is optional (--flag value also works).
Quick reference — common workflows
1. Run a GoogleSQL query
bq query --nouse_legacy_sql \
'SELECT word, SUM(word_count) AS count
FROM `bigquery-public-data.samples.shakespeare`
GROUP BY word ORDER BY count DESC LIMIT 10'
bq --format=prettyjson query --nouse_legacy_sql -n 1000 'SELECT ...'
echo 'SELECT 1' | bq query --nouse_legacy_sql
2. Parameterized query
bq query --nouse_legacy_sql \
--parameter='min_count:INT64:100' \
--parameter='corpus::hamlet' \
'SELECT word FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @corpus AND word_count >= @min_count'
3. Dry run — estimate bytes billed without running
bq query --nouse_legacy_sql --dry_run \
'SELECT * FROM `myproject.mydataset.mytable`'
4. Create a dataset and a table
bq mk -d --data_location=EU --default_table_expiration=3600 \
--description="Working dataset" mydataset
bq mk -t mydataset.mytable name:STRING,value:INTEGER,ts:TIMESTAMP
bq mk -t --time_partitioning_field=ts --time_partitioning_type=DAY \
--clustering_fields=name mydataset.events name:STRING,ts:TIMESTAMP
5. Load data (CSV / JSON, schema or autodetect)
bq load --source_format=CSV --skip_leading_rows=1 \
mydataset.mytable gs://mybucket/data.csv name:STRING,value:INTEGER
bq load --source_format=NEWLINE_DELIMITED_JSON --autodetect \
mydataset.mytable ./data.json
bq load --replace --source_format=CSV \
mydataset.mytable gs://mybucket/data.csv ./schema.json
6. Extract a table to Cloud Storage
bq extract mydataset.mytable gs://mybucket/export/table.csv
bq extract --destination_format=NEWLINE_DELIMITED_JSON --compression=GZIP \
mydataset.mytable 'gs://mybucket/export/table_*.json.gz'
7. Copy tables
bq cp mydataset.mytable mydataset2.mytable_copy
bq cp -a mydataset.src mydataset.dest
bq cp --snapshot --expiration=86400 mydataset.t mydataset.t_snap
bq cp --clone mydataset.t mydataset.t_clone
8. List, show, and preview
bq ls
bq ls mydataset
bq ls -p -n 1000
bq show mydataset.mytable
bq show --schema --format=prettyjson mydataset.mytable
bq head -n 10 mydataset.mytable
bq head -s 5 -n 10 -c 'name,value' mydataset.mytable
9. Stream rows into a table
bq insert mydataset.mytable ./rows.json
echo '{"name":"a","value":1}' | bq insert mydataset.mytable
10. Manage jobs
bq ls -j -n 20
bq ls -j --filter='states:RUNNING,PENDING' proj
bq show -j --location=US job_id
bq wait --fail_on_error job_id 300
bq cancel job_id
bq --nosync cancel job_id
11. IAM on tables and views
bq get-iam-policy mydataset.mytable
bq add-iam-policy-binding \
--member='user:someone@example.com' \
--role='roles/bigquery.dataViewer' \
mydataset.mytable
bq remove-iam-policy-binding \
--member='user:someone@example.com' \
--role='roles/bigquery.dataViewer' \
mydataset.mytable
bq set-iam-policy mydataset.mytable policy.json
12. External tables via mkdef
bq mkdef --source_format=CSV --autodetect 'gs://mybucket/data/*.csv' > tabledef.json
bq mk --external_table_definition=tabledef.json mydataset.exttable
bq mk --external_table_definition='name:STRING@CSV=gs://mybucket/data.csv' \
mydataset.exttable
Commands
All 26 commands are documented in commands.md; see index.md for the one-line list. Core surface: query, load, extract, cp, mk, mkdef, update, rm, show, ls, head, insert, wait, cancel, partition, truncate, undelete, get-iam-policy, set-iam-policy, add-iam-policy-binding, remove-iam-policy-binding, init, shell, info, version, help.
Common flags & tips
Official documentation