快速开始
10 分钟快速入门

10 分钟快速入门

本文为您介绍 ProtonBase 快速上手的操作步骤,帮助您了解如何创建 Data Cloud、创建 Warebase、获取连接信息以及创建数据库进行数据写入和查询。

一、前置条件

1. 注册 ProtonBase 账号

前往 http://cn-app.protonbase.com/signup (opens in a new tab) 进入 ProtonBase 注册页,新用户使用邮箱进行注册。

注册完成后,您将收到一封验证邮件,请点击邮件中的链接完成账号验证。

2. 安装 psql 终端

参考 PostgreSQL 官网提供的客户端安装方法 (opens in a new tab) 安装 PSQL。

提示:ProtonBase 完全兼容 PostgreSQL 协议,因此您可以使用任何支持 PostgreSQL 的客户端工具连接 ProtonBase。

二、开通服务

1. 创建 Data Cloud

Data Cloud 是您使用和管理数据库存储和计算资源的实体,您对数据的所有操作都需要在 Data Cloud 中完成。更多信息请参考 Data Cloud

请登录 https://cn-app.protonbase.com (opens in a new tab) 访问 ProtonBase WEB 界面,打开 Data Cloud 管理页创建新的 Data Cloud,后续操作就可以在对应的 Data Cloud Center 完成。

最佳实践:建议为不同的项目或环境(如开发、测试、生产)创建独立的 Data Cloud,以便更好地管理和隔离资源。

2. 创建 Warebase

Warebase 是 ProtonBase 的计算模块,您对数据库的所有 DDL、DML 等操作均在 Warebase 中完成,进行 SQL 操作之前请创建 Warebase,更多信息请参考 Warebase

您可以通过以下 2 个方式进入 Warebase 管理页点击"+Warebase"以新建 Warebase:

提示:创建 Warebase 时,请根据您的业务需求选择合适的规格。对于测试环境,可以选择较小的规格以节省成本。

三、连接 Warebase

Warebase 默认开启的是 VPC 网络访问,本案例中请使用公网访问。

在 Warebases 详情页,点击网络链接,开启公网访问,即可获取 Warebase 的公网域名和端口,同时获得 psql 连接信息。

psql 连接格式:psql -U <Account> -d postgres -h <Host> -p 5432

  • <Account>:您的 ProtonBase 账号
  • <Host>:Warebase 公网连接域名

安全提示:在生产环境中,建议使用 VPC 网络连接以提高安全性。公网访问仅适用于测试和开发环境。

四、创建对象

创建数据库

使用 CREATE DATABASE 命令创建 pb_test 数据库:

CREATE DATABASE pb_test;

通过以下命令切换到刚刚创建的 pb_test 数据库:

postgres=# \c pb_test
You are now connected to database "pb_test" as user "your_username".
pb_test=#

创建数据表

使用 CREATE TABLE 命令创建 cars 表:

CREATE TABLE cars (
  id SERIAL PRIMARY KEY,
  brand VARCHAR(255) NOT NULL,
  model VARCHAR(255) NOT NULL,
  year INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

执行结果如下:

pb_test=# CREATE TABLE cars (
  id SERIAL PRIMARY KEY,
  brand VARCHAR(255) NOT NULL,
  model VARCHAR(255) NOT NULL,
  year INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
pb_test=# \d cars
                           Table "public.cars"
   Column   |            Type             | Collation | Nullable |      Default
------------+-----------------------------+-----------+----------+-------------------
 id         | integer                     |           | not null | generated by default as identity
 brand      | character varying(255)      |           | not null |
 model      | character varying(255)      |           | not null |
 year       | integer                     |           |          |
 created_at | timestamp without time zone |           |          | CURRENT_TIMESTAMP
Indexes:
    "cars_pkey" PRIMARY KEY, btree (id)
 
pb_test=#

五、数据操作

写入数据

使用 INSERT INTO 命令往 cars 表写入数据:

INSERT INTO cars (brand, model, year)
VALUES
  ('Ford', 'Mustang', 1964),
  ('Tesla', 'Model S', 2020),
  ('BMW', 'X5', 2021);

执行结果如下:

pb_test=# INSERT INTO cars (brand, model, year)
VALUES
  ('Ford', 'Mustang', 1964),
  ('Tesla', 'Model S', 2020),
  ('BMW', 'X5', 2021);
INSERT 0 3

查询数据

使用 SELECT 命令查询数据:

-- 查询所有数据
SELECT * FROM cars;
 
-- 按年份排序查询
SELECT brand, model, year FROM cars ORDER BY year;
 
-- 条件查询
SELECT * FROM cars WHERE year > 2000;

执行结果如下:

pb_test=# SELECT * FROM cars;
 id | brand |  model  | year |     created_at
----+-------+---------+------+---------------------
  1 | Ford  | Mustang | 1964 | 2023-01-01 10:00:00
  2 | Tesla | Model S | 2020 | 2023-01-01 10:00:01
  3 | BMW   | X5      | 2021 | 2023-01-01 10:00:02
(3 rows)
 
pb_test=# SELECT brand, model, year FROM cars ORDER BY year;
 brand |  model  | year
-------+---------+------
 Ford  | Mustang | 1964
 Tesla | Model S | 2020
 BMW   | X5      | 2021
(3 rows)
 
pb_test=# SELECT * FROM cars WHERE year > 2000;
 id | brand |  model  | year |     created_at
----+-------+---------+------+---------------------
  2 | Tesla | Model S | 2020 | 2023-01-01 10:00:01
  3 | BMW   | X5      | 2021 | 2023-01-01 10:00:02
(2 rows)

更新数据

使用 UPDATE 命令更新已有数据:

UPDATE cars
SET model = 'Mustang GT'
WHERE brand = 'Ford';

执行结果如下:

pb_test=# UPDATE cars
SET model = 'Mustang GT'
WHERE brand = 'Ford';
UPDATE 1
pb_test=# SELECT * FROM cars WHERE brand = 'Ford';
 id | brand |   model   | year |     created_at
----+-------+-----------+------+---------------------
  1 | Ford  | Mustang GT| 1964 | 2023-01-01 10:00:00
(1 row)

删除数据

使用 DELETE 命令删除数据:

DELETE FROM cars WHERE year < 2000;

执行结果如下:

pb_test=# DELETE FROM cars WHERE year < 2000;
DELETE 1
pb_test=# SELECT * FROM cars;
 id | brand |  model  | year |     created_at
----+-------+---------+------+---------------------
  2 | Tesla | Model S | 2020 | 2023-01-01 10:00:01
  3 | BMW   | X5      | 2021 | 2023-01-01 10:00:02
(2 rows)

六、下一步

恭喜您已完成 ProtonBase 的快速入门!您现在已经掌握了:

  • 如何创建和管理 Data Cloud 和 Warebase
  • 如何连接到 ProtonBase
  • 如何创建数据库和表
  • 如何进行基本的数据操作(增删改查)

接下来,您可以:

如果您有任何问题,请联系技术支持。