yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务

2022-10-08 18:57:37 +08:00
 ggvm

yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务。

免费申请到的资源如下:

申请链接: https://cloud.yugabyte.com/

步骤:

1 输入企业邮箱地址和密码注册

2 选择免费套餐:

可以看到免费可以得到的资源 2CPU/4G RAM/10G storage 。500 张表,12.5 百万行数据

3 选择区域

可以选择 AWS 或者 GCP ,根据经验,AWS 日本到中国线路比较优秀,有 AWS 日本当然可以优先选择。

4 选择好区域后,创建中

<figure class="wp-block-image size-full"></figure>

5 创建成功

大约等待 6-10 分钟,数据库就创建好了。创建成功后,系统页面引导我们使用数据库。

<figure class="wp-block-image size-full"></figure>

6 添加允许访问的 IP 和 IP 段。

如果希望所有 ip 都能访问,填写 0.0.0.0/0

<figure class="wp-block-image size-full"></figure>

7 获取链接方式

连接需要下载一个 CA 文件,例如为 root.crt

<figure class="wp-block-image size-full"></figure>

8 测试使用

首先需要安装 psycopg2

pip3 install psycopg2-binary

或者参考你的 python 文档

import psycopg2
import psycopg2.extras

config = {
    'host': 'ap-northeast-1.xxxxxxx.aws.ybdb.io',
    'port': '5433',
    'dbName': 'yugabyte',
    'dbUser': 'admin',
    'dbPassword': 'Ckw9F-xxxxxxx',
    'sslMode': 'verify-full',
    'sslRootCert': './root.crt'
}


def main(conf):
    print(">>>> Connecting to YugabyteDB!")

    try:
        if conf['sslMode'] != '':
            yb = psycopg2.connect(host=conf['host'], port=conf['port'], database=conf['dbName'],
                                  user=conf['dbUser'], password=conf['dbPassword'],
                                  sslmode=conf['sslMode'], sslrootcert=conf['sslRootCert'],
                                  connect_timeout=10)
        else:
            yb = psycopg2.connect(host=conf['host'], port=conf['port'], database=conf['dbName'],
                                  user=conf['dbUser'], password=conf['dbPassword'],
                                  connect_timeout=10)
    except Exception as e:
        print("Exception while connecting to YugabyteDB")
        print(e)
        exit(1)

    print(">>>> Successfully connected to YugabyteDB!")

    #create_database(yb)
    #select_accounts(yb)
    #transfer_money_between_accounts(yb, 800)
    select_accounts(yb)
    yb.close()


def create_database(yb):
    try:
        with yb.cursor() as yb_cursor:
            yb_cursor.execute('DROP TABLE IF EXISTS DemoAccount')

            create_table_stmt = """
                CREATE TABLE DemoAccount (
                    id int PRIMARY KEY,
                    name varchar,
                    age int,
                    country varchar,
                    balance int
                )"""
            yb_cursor.execute(create_table_stmt)

            insert_stmt = """
                INSERT INTO DemoAccount VALUES
                        (1, 'Jessica', 28, 'USA', 10000),
                        (2, 'John', 28, 'Canada', 9000)"""
            yb_cursor.execute(insert_stmt)
        yb.commit()
    except Exception as e:
        print("Exception while creating tables")
        print(e)
        exit(1)

    print(">>>> Successfully created table DemoAccount.")


def select_accounts(yb):
    print(">>>> Selecting accounts:")

    with yb.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as yb_cursor:
        yb_cursor.execute("SELECT name, age, country, balance FROM DemoAccount")

        results = yb_cursor.fetchall()
        for row in results:
            print("name = {name}, age = {age}, country = {country}, balance = {balance}".format(**row))


def transfer_money_between_accounts(yb, amount):
    try:
        with yb.cursor() as yb_cursor:
            yb_cursor.execute("UPDATE DemoAccount SET balance = balance - %s WHERE name = 'Jessica'", [amount])
            yb_cursor.execute("UPDATE DemoAccount SET balance = balance + %s WHERE name = 'John'", [amount])
        yb.commit()
    except (Exception, psycopg2.DatabaseError) as e:
        print("Exception while transferring money")
        print(e)
        if e.pgcode == 40001:
            print("The operation is aborted due to a concurrent transaction that is modifying the same set of rows." +
                  "Consider adding retry logic or using the pessimistic locking.")
        exit(1)

    print(">>>> Transferred {} between accounts.".format(amount))


if __name__ == "__main__":
    main(config)

我使用的是绿云 VPS提供的大阪的服务器,测试起来相当流畅。

看到访问的速度还很不错。

总结:

yugabyteDB 为开发者免费提供 2 CPU 4G RAM 10G 存储的 postgreSQL 云服务。

虽然存储的总行数不能超过 1.25 千万,但对于一般的使用已经足够。

yugabyteDB 提供了 AWS 和 GCP 的众多数据中心,满足了很多 VPS 群友缺少存储服务器的需求。

更多免费数据库和 vps 资源有兴趣可了解 https://zhuji188.com/780.html

1542 次点击
所在节点    分享发现
5 条回复
mayli
2022-10-09 00:27:24 +08:00
会跑路吗?
ggvm
2022-10-09 08:49:15 +08:00
@mayli 暂时看不到这个迹象
luojianxhlxt
2022-10-09 09:32:19 +08:00
企业邮箱地址,这个怎么判断呀
ggvm
2022-10-09 09:55:03 +08:00
@luojianxhlxt 自己搞个域名就是
Wincer
2022-10-09 12:32:34 +08:00
aws 的 region 后续可以免费更换成其它的吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/885350

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX