免费的数据库DBaaS之 yugabyteDB — 云postgresql 限额10G存储

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 选择好区域后,创建中

5 创建成功

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

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

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

7 获取链接方式

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

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群友缺少存储服务器的需求。

主机差评君首发于zhuji188.com,转发请标明来源https://zhuji188.com/780.html
THE END
分享
二维码
< <上一篇
下一篇>>