如何从Hasura数据库及集群内的数据表导出为CSV文件?
Hey there! Exporting a table from Hasura to CSV is totally doable, and I’ve got a couple of straightforward methods for you depending on what you prefer—UI-based or command-line.
方法1:通过Hasura Console(可视化操作,适合快速导出)
This is the easiest way if you don’t want to mess with commands:
- Open your Hasura Console and navigate to the Data tab.
- Find the table you want to export in the left sidebar menu and click into its details page.
- Switch to the Rows tab, where you’ll see all the data in the table.
- Look for the Export button (usually a download icon or clearly labeled) in the top-right corner. Click it, select CSV as the format, and your browser will automatically download the CSV file.
- Pro tip: If you only need a subset of the data, use the Filter bar at the top to narrow down the rows first—then export, and your CSV will only include the filtered data.
方法2:通过GraphQL API + 命令行(适合自动化/批量导出)
Great if you need to script the export process or handle it remotely:
- First, build a GraphQL query to fetch all the data from your target table. For example, if your table is named
products, the query might look like this:
query ExportProducts { products { id name price in_stock created_at } }
- Use
curlto call your Hasura GraphQL endpoint, and pipe the JSON response throughjq(you’ll needjqinstalled first) to convert it to CSV:
curl -X POST \ -H "Content-Type: application/json" \ -H "x-hasura-admin-secret: YOUR_ADMIN_SECRET" \ http://your-hasura-instance-url/v1/graphql \ -d '{"query":"query ExportProducts { products { id name price in_stock created_at } }"}' \ | jq -r '.data.products | (first | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' > products_export.csv
- Breakdown: The
jqcommand first extracts the column names as CSV headers, then converts each row of JSON data into a CSV row, saving everything to a local file. Don’t forget to replaceYOUR_ADMIN_SECRETand your Hasura instance URL with your actual details, and adjust the query fields to match your table.
方法3:直接连接数据库导出(适合大数据量表)
If you have direct access to the underlying database (most commonly PostgreSQL for Hasura), this is the fastest method for large datasets:
- Connect to your database using the
psqlclient:
psql -h YOUR_DB_HOST -U YOUR_DB_USER -d YOUR_DB_NAME
- Run the export command. To save the file directly on the database server:
COPY products TO '/path/on/server/products_export.csv' WITH (FORMAT CSV, HEADER, DELIMITER ',');
- If you want to save the file locally (for remote databases), use the
\copycommand instead:
\copy products TO '/path/on/your/local/machine/products_export.csv' WITH (FORMAT CSV, HEADER);
- Note: This requires you to have the necessary database permissions, but it’s way faster for tables with millions of rows.
内容的提问来源于stack exchange,提问作者sandip
相关产品推荐
相关产品推荐

