Membuat dan mengkueri database AlloyDB menggunakan konsol Google Cloud

Untuk mulai menggunakan AlloyDB untuk PostgreSQL di konsol Google Cloud , Anda dapat membuat cluster dan instance utama, terhubung ke instance menggunakan AlloyDB Studio, serta menyisipkan dan mengkueri data sampel. Di AlloyDB, database Anda disimpan dalam cluster yang berisi instance. Untuk mengetahui informasi selengkapnya tentang arsitektur, cluster, dan instance AlloyDB, lihat Ringkasan AlloyDB.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  10. Service Networking API diperlukan jika Anda berencana mengonfigurasi konektivitas jaringan ke AlloyDB menggunakan jaringan Virtual Private Cloud (VPC) yang berada di project Google Cloud yang sama dengan AlloyDB.

    Compute Engine API dan Cloud Resource Manager API diperlukan jika Anda berencana mengonfigurasi konektivitas jaringan ke AlloyDB menggunakan jaringan VPC yang berada di project Google Cloud yang berbeda.

  11. Pilih opsi konektivitas.

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk membuat dan terhubung ke database AlloyDB, minta administrator untuk memberi Anda peran IAM berikut pada project Anda:

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran yang telah ditetapkan ini berisi izin yang diperlukan untuk membuat dan terhubung ke database AlloyDB. Untuk melihat izin yang benar-benar diperlukan, perluas bagian Izin yang diperlukan:

Izin yang diperlukan

Izin berikut diperlukan untuk membuat dan terhubung ke database AlloyDB:

  • compute.networks.list
  • compute.networks.addPeering
  • compute.addresses.create
  • servicenetworking.services.addPeering
  • alloydb.clusters.create
  • alloydb.instances.create
  • alloydb.instances.login

Anda mungkin juga bisa mendapatkan izin ini dengan peran khusus atau peran bawaan lainnya.

Membuat cluster dan instance AlloyDB utamanya

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Klik Create cluster.

  3. Di Configure your cluster, ikuti langkah-langkah berikut:

    1. Di kolom Cluster ID, masukkan my-cluster.

    2. Masukkan sandi. Catat sandi ini karena Anda akan menggunakannya dalam panduan memulai ini.

    3. Pertahankan versi database default.

    4. Di menu Region di bagian Location, pilih region us-central1 (Iowa).

  4. Di bagian Configure your primary instance, ikuti langkah-langkah berikut:

    1. Di kolom ID Instance, masukkan my-cluster-primary.

    2. Di Zonal availability, pilih Single zone.

    3. Di menu Machine type, pilih jenis mesin 2 vCPU, 16 GB.

    4. Di menu Network di bagian Connectivity, pilih jaringan default.

      Jika Anda telah mengonfigurasi koneksi akses layanan pribadi, lanjutkan ke langkah berikutnya. Jika tidak, klik Set up connection dan ikuti langkah-langkah berikut:

      1. Di Allocate an IP range, pilih Use an automatically allocated IP range.

      2. Klik Continue, lalu klik Create connection.

    5. Pertahankan setelan default di bagian keamanan jaringan.

  5. Klik Create cluster. Mungkin perlu waktu beberapa menit hingga AlloyDB membuat cluster dan menampilkannya di halaman Overview cluster utama.

Menghubungkan ke instance dan membuat database di AlloyDB Studio

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Klik nama cluster Anda, my-cluster, di kolom Resource name.

  3. Di panel navigasi, klik AlloyDB Studio.

  4. Di halaman Sign in to AlloyDB Studio, ikuti langkah-langkah berikut:

    1. Pilih metode autentikasi database.

    2. Pilih database postgres.

    3. Klik Authenticate. Panel Explorer menampilkan daftar objek dalam database postgres.

  5. Untuk membuat database, masukkan pernyataan berikut di tab Editor 1, lalu klik Run:

    CREATE DATABASE guestbook;
    

    Anda akan tahu bahwa database telah dibuat saat pesan Statement executed successfully ditampilkan di panel Results.

Menghubungkan ke database guestbook

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Klik nama cluster Anda, my-cluster, di kolom Resource name.

  3. Di panel navigasi, klik AlloyDB Studio.

  4. Di halaman Sign in to AlloyDB Studio, ikuti langkah-langkah berikut:

    1. Pilih database guestbook.

    2. Pilih pengguna postgres.

    3. Masukkan sandi yang Anda buat di Membuat cluster dan instance AlloyDB utamanya.

    4. Klik Authenticate. Panel Explorer menampilkan daftar objek dalam database guestbook.

Memverifikasi koneksi database Anda

  1. Ikuti langkah-langkah di Menghubungkan ke database guestbook.

  2. Untuk menyisipkan data sampel ke dalam database guestbook, masukkan kode berikut di tab Editor 1:

    CREATE TABLE entries (guestName VARCHAR(255),
                          content VARCHAR(255),
                          entryID SERIAL PRIMARY KEY);
    INSERT INTO entries (guestName, content) values ('Alex', 'I got here!');
    INSERT INTO entries (guestName, content) values ('Kai', 'Me too!');
    
  3. Klik Run. Pesan Statement executed successfully di panel Results menunjukkan bahwa data disisipkan ke dalam database Anda.

  4. Untuk menghapus teks dari tab Editor 1 agar Anda dapat menjalankan perintah lain, klik Clear.

  5. Untuk memilih semua entri dalam database, masukkan kueri berikut di tab Editor 1:

    SELECT * FROM entries;
    
  6. Klik Run. Output yang mirip dengan berikut ini akan muncul di panel Results:

        guestname     | content     | entryid
        --------------+-------------+---------
        Alex          | I got here! | 1
        Kai           | Me too!     | 2
    

Pembersihan

Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Klik nama cluster Anda, my-cluster, di kolom Resource name.

  3. Klik Delete cluster.

  4. Di Delete cluster my-cluster, masukkan my-cluster untuk mengonfirmasi bahwa Anda ingin menghapus cluster.

  5. Klik Hapus.

  6. Opsional: Jika Anda membuat koneksi akses layanan pribadi saat membuat cluster dan tidak lagi memerlukan rentang alamat IP yang dialokasikan, ikuti langkah-langkah dalam Menghapus rentang yang dialokasikan akses layanan pribadi.

Langkah berikutnya