将数据导入 Supabase
你可以通过多种方式将数据导入 Supabase。最好的方法取决于你的数据大小和应用需求。
🌐 You can import data into Supabase in multiple ways. The best method depends on your data size and app requirements.
如果你在开发中处理小型数据集,可以在 Supabase 仪表板上尝试 CSV 导入。如果你在生产中处理大型数据集,你应该规划数据导入,以尽量减少应用延迟并确保数据完整性。
🌐 If you're working with small datasets in development, you can experiment with CSV import in the Supabase dashboard. If you're working with a large dataset in production, you should plan your data import to minimize app latency and ensure data integrity.
如何把数据导入 Supabase #
🌐 How to import data into Supabase
你有多种方式可以将数据导入 Supabase:
🌐 You have multiple options for importing your data into Supabase:
如果你要导入一个大型数据集或将数据导入到生产环境,请提前计划并准备好你的数据库。
🌐 If you're importing a large dataset or importing data into production, plan ahead and prepare your database.
选项1:通过 Supabase 仪表板导入 CSV #
🌐 Option 1: CSV import via Supabase dashboard
Supabase 仪表板提供了一种用户友好的方式来导入数据。不过,对于非常大的数据集来说,这种方法可能不是最有效的选择,因为大小限制是 100MB。它通常更适合较小的数据集和快速数据导入。对于大规模数据导入,可以考虑使用像 pgloader 这样的替代方法。
🌐 Supabase dashboard provides a user-friendly way to import data. However, for very large datasets, this method may not be the most efficient choice, given the size limit is 100MB. It's generally better suited for smaller datasets and quick data imports. Consider using alternative methods like pgloader for large-scale data imports.
- 导航到表格编辑器中的相关表格
- 点击 + 新建表格(用于新的空项目)或 插入(用于现有表格),然后选择 从 CSV 导入数据,按照屏幕上的指示上传你的 CSV 文件。
选项 2:使用 pgloader 批量导入 #
🌐 Option 2: Bulk import using pgloader
pgloader 是一个强大的工具,可以高效地将数据导入支持多种源数据库引擎(包括 MySQL 和 MS SQL)的 Postgres 数据库。
你可以按照以下步骤将它与 Supabase 一起使用:
🌐 You can use it in conjunction with Supabase by following these steps:
-
在你的本地电脑或服务器上安装 pgloader。更多信息,你可以参考官方 pgloader 安装页面。
1$ apt-get install pgloader -
创建一个配置文件,指定源数据和目标 Supabase 数据库(例如,config.load)。这是一个示例配置文件:
1LOAD DATABASE2FROM sourcedb://USER:PASSWORD@HOST/SOURCE_DB3INTO postgres://postgres.xxxx:password@xxxx.pooler.supabase.com:6543/postgres4ALTER SCHEMA 'public' OWNER TO 'postgres';5set wal_buffers = '64MB', max_wal_senders = 0, statement_timeout = 0, work_mem to '2GB';自定义源和 Supabase 数据库 URL 以及选项,以适应你的具体使用场景:
wal_buffers:这个参数设置为 '64MB',用于为预写日志缓冲区分配 64 兆字节的内存。更大的值可以通过在写入磁盘前缓存更多数据来提高写入性能。在数据导入操作中,这可以加快事务日志的写入速度。max_wal_senders:它被设置为0,以禁用复制连接。这是在数据导入过程中完成的,以防止与复制相关的冲突和问题。statement_timeout:这个值被设置为0,这意味着它被禁用,可以让 SQL 语句无限制地运行。work_mem:它被设置为“2GB”,为查询操作分配 2 GB 的内存。这样可以通过允许更大的内存数据集来提升复杂查询的性能。
-
使用配置文件运行 pgloader。
1pgloader config.load
对于使用 Postgres 引擎的数据库,我们推荐使用 pg_dump 和 psql 命令行工具。
🌐 For databases using the Postgres engine, we recommend using the pg_dump and psql command line tools.
选项3:使用Postgres的copy命令 #
🌐 Option 3: Using Postgres copy command
阅读更多关于批量数据加载的信息。
🌐 Read more about Bulk data loading.
选项4:使用Supabase API #
🌐 Option 4: Using the Supabase API
Supabase API 允许你以编程方式将数据导入到你的表中。你可以使用各种客户端库与 API 进行交互并执行数据导入操作。当你需要自动化数据导入时,这种方法非常有用,而且它能让你对整个过程进行精细控制。更多详情请参考我们的 API 指南。
🌐 The Supabase API allows you to programmatically import data into your tables. You can use various client libraries to interact with the API and perform data import operations. This approach is useful when you need to automate data imports, and it gives you fine-grained control over the process. Refer to our API guide for more details.
通过 Supabase API 导入数据时,建议不要进行批量导入。这有助于确保数据传输顺利,并避免潜在的中断。
🌐 When importing data via the Supabase API, it's advisable to refrain from bulk imports. This helps ensure a smooth data transfer process and prevents any potential disruptions.
阅读更多关于速率限制、资源分配和防滥用的信息。
🌐 Read more about Rate Limiting, Resource Allocation, & Abuse Prevention.
准备导入数据 #
🌐 Preparing to import data
大量数据导入可能会影响你的数据库性能。导入失败也可能导致数据损坏。导入数据是一项安全且常见的操作,但如果你要导入大量数据,或者在生产环境中工作,你最好提前做好计划。
🌐 Large data imports can affect your database performance. Failed imports can also cause data corruption. Importing data is a safe and common operation, but you should plan ahead if you're importing a lot of data, or if you're working in a production environment.
1. 备份你的数据 #
🌐 1. Back up your data
备份可以帮助你在出现问题时恢复数据。Pro、Team 和 Enterprise 计划的数据库会按计划自动备份,但你也可以自己进行备份。更多信息请查看 数据库备份。
🌐 Backups help you restore your data if something goes wrong. Databases on Pro, Team and Enterprise Plans are automatically backed up on schedule, but you can also take your own backup. See Database Backups for more information.
2. 增加语句超时 #
🌐 2. Increase statement timeouts
默认情况下,Supabase 会强制执行查询语句超时,以确保资源的公平分配并防止长时间运行的查询影响整个系统。在导入大型数据集时,你可能会遇到超时。为了解决这个问题:
🌐 By default, Supabase enforces query statement timeouts to ensure fair resource allocation and prevent long-running queries from affecting the overall system. When importing large datasets, you may encounter timeouts. To address this:
- 增加语句超时:你可以调整会话或连接的语句超时,以适应运行时间较长的查询。但在这样做时要小心,因为过长的查询可能会对系统性能产生负面影响。了解更多关于语句超时的信息。
3. 估算你需要的磁盘大小 #
🌐 3. Estimate your required disk size
大型数据集会占用磁盘空间。确保你的 Supabase 项目有足够的磁盘容量来容纳导入的数据。如果你知道数据库将会有多大,可以在你的 项目数据库设置 中手动增加容量。
🌐 Large datasets consume disk space. Ensure your Supabase project has sufficient disk capacity to accommodate the imported data. If you know how big your database is going to be, you can manually increase the size in your projects database settings.
阅读更多关于disk management的信息。
🌐 Read more about disk management.
4. 禁用触发器 #
🌐 4. Disable triggers
在导入大数据集时,临时禁用触发器通常会很有帮助。触发器可能会显著减慢导入速度,尤其是当它们涉及复杂逻辑或参照完整性检查时。导入完成后,你可以重新启用触发器。
🌐 When importing large datasets, it's often beneficial to disable triggers temporarily. Triggers can significantly slow down the import process, especially if they involve complex logic or referential integrity checks. After the import, you can re-enable the triggers.
要禁用触发器,请使用以下 SQL 命令:
🌐 To disable triggers, use the following SQL commands:
1-- Disable triggers on a specific table2ALTER TABLE table_name DISABLE TRIGGER ALL;34-- To re-enable triggers5ALTER TABLE table_name ENABLE TRIGGER ALL;5. 数据导入完成后重建索引 #
🌐 5. Rebuild indices after data import is complete
索引对于查询性能非常重要,但在导入大数据集时建立索引可能会很耗时间。可以考虑在数据导入完成后再建立或重建索引。这种做法可以显著加快导入过程,减少总耗时。
🌐 Indexing is crucial for query performance, but building indices while importing a large dataset can be time-consuming. Consider building or rebuilding indices after the data import is complete. This approach can significantly speed up the import process and reduce the overall time required.
在数据导入后建立索引:
🌐 To build an index after the data import:
1-- Create an index on a table2create index index_name on table_name (column_name);阅读更多关于 在 Postgres 中管理索引 的内容。
🌐 Read more about Managing Indexes in Postgres.