データベースを作成して接続する
このページでは、AlloyDB for PostgreSQL で最初のデータベースを作成して接続するプロセスについて説明します。
このクイックスタートの最後では、ここで作成したデータベースをクリーンアップする手順を説明します。このページの最後にある「次のステップ」では、独自のアプリケーションで AlloyDB を使用する方法を紹介するリンクを示します。
始める前に
コンソール
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
 AlloyDB for PostgreSQL の作成と接続に必要な Cloud APIs を有効にします。 - [プロジェクトを確認] の手順で、[次へ] をクリックして、変更するプロジェクトの名前を確認します。 
- [API を有効にする] の手順で、[有効にする] をクリックして、次の機能を有効にします。 - AlloyDB API
- Compute Engine API
- Cloud Resource Manager API
- Service Networking API
 - AlloyDB と同じ Google Cloud プロジェクトにある VPC ネットワークを使用して AlloyDB へのネットワーク接続を構成する場合は、Service Networking API が必要です。 - 別の Google Cloud プロジェクトに存在する VPC ネットワークを使用して AlloyDB へのネットワーク接続を構成する場合は、Compute Engine API と Cloud Resource Manager API が必要です。 
 
- AlloyDB への接続方法を選択します。
gcloud
- Google Cloud プロジェクトを作成するか、既存のプロジェクトを使用します。 - gcloud projects create PROJECT_ID --set-as-default # For an existing project gcloud config set project PROJECT_ID- 次のように置き換えます。 - PROJECT_ID: プロジェクトの永続的な識別子。
 
- Google Cloud プロジェクトに対して課金が有効になっていることを確認します。プロジェクトに対して課金が有効になっていることを確認する方法をご覧ください。 
- AlloyDB、Compute Engine、Resource Manager API を有効にします。 - gcloud services enable alloydb.googleapis.com gcloud services enable compute.googleapis.com gcloud services enable cloudresourcemanager.googleapis.com
- AlloyDB と同じ Google Cloud プロジェクトに存在する VPC ネットワークを使用して AlloyDB へのネットワーク接続を構成する場合は、プロジェクトで Service Networking API を有効にします。 - gcloud services enable servicenetworking.googleapis.com
- 別の Google Cloud プロジェクトにある VPC ネットワークを使用して AlloyDB へのネットワーク接続を構成する場合は、そのプロジェクトで共有 VPC とプライベート サービス アクセスを構成できるように、そのプロジェクトの Compute Engine と Service Networking API が有効になっていることを確認します。 - gcloud services list
クラスタとそのプライマリ インスタンスを作成する
- Google Cloud コンソールで、[クラスタ] ページに移動します。 
- [クラスタを作成] をクリックします。 
- [クラスタを構成する] で、次の操作を行います。 - [クラスタ ID] フィールドに「 - my-cluster」と入力します。
- パスワードを入力します。このクイックスタートで使用するため、このパスワードはメモしておきます。 
- デフォルトのデータベース バージョンはそのままにします。 
- [ロケーション] の [リージョン] プルダウンで、 - us-central1 (Iowa)リージョンを選択します。
 
- ]プライマリ インスタンスの構成] で、次の操作を行います。 - [インスタンス ID] フィールドに「 - my-cluster-primary」と入力します。
- [ゾーンの可用性] で、[シングルゾーン] をクリックします。 
- [マシンタイプ] プルダウンで、 - 2 vCPU, 16 GBマシンタイプを選択します。
- [接続] の [ネットワーク] プルダウンで、 - defaultネットワークを選択します。- プライベート アクセス接続がある場合は、クラスタの作成ステップに進みます。接続が設定されていない場合は、[接続を設定] をクリックして、次の操作を行います。 - [IP 範囲を割り振る] で、[自動的に割り当てられた IP 範囲を使用する] をクリックします。 
- [続行]、[接続を作成] の順にクリックします。 
 
- [ネットワーク セキュリティ] のデフォルト設定をそのまま使用します。 
 
- [クラスタを作成] をクリックします。AlloyDB がクラスタを作成して、プライマリ クラスタの [概要] ページに表示されるまでに数分かかることがあります。 
インスタンスに接続してデータベースを作成する
- Google Cloud コンソールで、[クラスタ] ページに移動します。 
- [リソース名] 列でクラスタ名 - my-clusterをクリックします。
- ナビゲーション パネルで [AlloyDB Studio] をクリックします。 
- [AlloyDB Studio にログインする] ページで、次の操作を行います。 - データベースの認証方法を選択します。 
- postgresデータベースを選択します。
- [認証] をクリックします。[エクスプローラ] ペインに、 - postgresデータベースにあるオブジェクトのリストが表示されます。
 
- データベースを作成するには、[Editor 1] タブに次のステートメントを入力し、[実行] をクリックします。 - CREATE DATABASE guestbook;- [結果] ペインに「 - Statement executed successfully」というメッセージが表示されたら、データベースが作成されています。
guestbook データベースに接続する
- Google Cloud コンソールで、[クラスタ] ページに移動します。 
- [リソース名] 列でクラスタ名 - my-clusterをクリックします。
- ナビゲーション パネルで [AlloyDB Studio] をクリックします。 
- [AlloyDB Studio にログインする] ページで、次の操作を行います。 - guestbookデータベースを選択します。
- postgresユーザーを選択します。
- クラスタとそのプライマリ インスタンスを作成するで作成したパスワードを入力します。 
- [認証] をクリックします。[エクスプローラ] ペインに、 - guestbookデータベースにあるオブジェクトのリストが表示されます。
 
データベース接続を確認する
- guestbookデータベースに接続するの手順を実施します。
- guestbookデータベースにサンプルデータを挿入するには、[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!');
- [実行] をクリックします。[結果] ペインに「 - Statement executed successfully」というメッセージが表示されたら、データがデータベースに挿入されています。
- 別のコマンドを実行できるように、[クリア] をクリックして、[Editor 1] タブからテキストを削除します。 
- データベース内のすべてのエントリを選択するには、[Editor 1] タブに次のクエリを入力します。 - SELECT * FROM entries;
- [実行] をクリックします。[結果] ペインに次のような出力が表示されます。 - guestname | content | entryid --------------+-------------+--------- Alex | I got here! | 1 Kai | Me too! | 2
クリーンアップする
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。
- Google Cloud コンソールで、[クラスタ] ページに移動します。 
- [リソース名] 列でクラスタ名 - my-clusterをクリックします。
- [deleteクラスタを削除] をクリックします。 
- [Delete cluster my-cluster] で「 - my-cluster」と入力して、クラスタの削除を確認します。
- [削除] をクリックします。 
- クラスタを作成する際にプライベート接続を作成した場合は、 Google Cloud コンソールの [ネットワーキング] ページに移動し、[VPC ネットワークの削除] をクリックします。