IBMCloudでIBM Cloud ShellでDatabases for PostgreSQLをプロビジョニングして接続してみる


概要

コマンドライン(ibmcloudコマンド)でPostgreSQLのサービスインスタンスをデプロイして接続してみます。
感想:コマンドのhelpを出力しながら実施しましたが、思ったよりシンプルで簡単に設定できました。

手順概要

  1. IBM Cloud Shellを起動
  2. ibmcloudコマンドにリソースグループを設定
  3. カタログを検索
  4. サービスインスタンスを作成
  5. PostgreSQLのadminユーザーのパスワードを設定
  6. adminユーザーで接続

1. IBM Cloud Shellを起動

IBMCloudのコンソールの右上にあるIBM CloudシェルのアイコンからIBM Cloud Shellを起動します。

2. IBM Cloud Shellにリソースグループを設定

IBM Cloud Shellの起動直後にはibmcloudコマンドにリソースグループの設定が入っていないので、設定します。

設定を確認
TSOTA@cloudshell:~$ ibmcloud target 


API endpoint:      https://cloud.ibm.com   
Region:            jp-tok   
User:              xxxx   
Account:           xxxx 
Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   
CF API endpoint:      
Org:                  
Space:
リソースグループを確認
TSOTA@cloudshell:~$ ibmcloud resource groups
Retrieving all resource groups under account xxxx as xxxx...
OK
Name      ID                                 Default Group   State   
Default   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   true            ACTIVE  
リソースグループを設定
TSOTA@cloudshell:~$ ibmcloud target -g Default
Targeted resource group Default

API endpoint:      https://cloud.ibm.com   
Region:            jp-tok   
User:              xxxx  
Account:           xxxx
Resource group:    Default   
CF API endpoint:      
Org:                  
Space:

カタログを検索

サービスインスタンスを作成する際にサービスIDとプランIDが必要になるため、カタログを検索して確認します。

postgreSQLでカタログを検索
TSOTA@cloudshell:~$ ibmcloud catalog search postgreSQL
Getting offerings...
OK
Name                                                                      ID                                     Kind   
Databases for EnterpriseDB                                                databases-for-enterprisedb-group       service   
Databases for PostgreSQL                                                  databases-for-postgresql-group         service   
Hyper Protect DBaaS for PostgreSQL                                        024d3260-5cf4-11e9-90a2-37a496589afc   service   
OSS Record: Compose for PostgreSQL(compose-for-postgresql)                oss.compose-for-postgresql             oss   
OSS Record: Databases for PostgreSQL(databases-for-postgresql)            oss.databases-for-postgresql           oss   
OSS Record: Hyper Protect DBaaS for PostgreSQL(hyperp-dbaas-postgresql)   oss.hyperp-dbaas-postgresql            oss   
TSOTA@cloudshell:~$ 
サービスの詳細を確認
TSOTA@cloudshell:~$ ibmcloud catalog service databases-for-postgresql-group  
Getting catalog entry...
OK

ID                 databases-for-postgresql-group   
Name               databases-for-postgresql-group   
Kind               service   
Provider           IBM   
Tags               apidocs_enabled, big_data, data_management, database, eu_access, gc_migrate, group, hipaa, ibm_created, service_endpoint_supported   
Active             true   
Description        PostgreSQL is a powerful, open source object-relational database that is highly customizable.   
Bindable           true   
Original Name         
RC Compatible      true   
RC Provisionable   false   
IAM Compatible     false   
Children           Name                        Kind         ID                                           Location   Original Location   Target      
                   compose-for-postgresql      service      ComposePostgreSQL-P                                                               
                   |__enterprise               plan         ComposePostgresqlEnterprise                                                       
                   |  |__enterprise-au-syd     deployment   ComposePostgresqlEnterprise:au-syd           au-syd                         bluemix-au-syd      
                   |  |__enterprise-eu-de      deployment   ComposePostgresqlEnterprise:eu-de            eu-de                          bluemix-eu-de      
                   |  |__enterprise-eu-gb      deployment   ComposePostgresqlEnterprise:eu-gb            eu-gb                          bluemix-eu-gb      
                   |  |__enterprise-us-east    deployment   ComposePostgresqlEnterprise:us-east          us-east                        bluemix-us-east      
                   |  |__enterprise-us-south   deployment   ComposePostgresqlEnterprise:us-south         us-south                       bluemix-us-south      
                   |__standard                 plan         ComposePostgresqlStandard                                                         
                      |__standard-au-syd       deployment   ComposePostgresqlStandard:au-syd             au-syd                         bluemix-au-syd      
                      |__standard-eu-de        deployment   ComposePostgresqlStandard:eu-de              eu-de                          bluemix-eu-de      
                      |__standard-eu-gb        deployment   ComposePostgresqlStandard:eu-gb              eu-gb                          bluemix-eu-gb      
                      |__standard-us-east      deployment   ComposePostgresqlStandard:us-east            us-east                        bluemix-us-east      
                      |__standard-us-south     deployment   ComposePostgresqlStandard:us-south           us-south                       bluemix-us-south      
                   databases-for-postgresql    service      databases-for-postgresql                                                          
                   |__standard                 plan         databases-for-postgresql-standard                                                 
                      |__standard-au-syd       deployment   databases-for-postgresql-standard:au-syd     au-syd                         bluemix-au-syd      
                      |__standard-che01        deployment   databases-for-postgresql-standard:che01      che01                          bluemix-che01      
                      |__standard-eu-de        deployment   databases-for-postgresql-standard:eu-de      eu-de                          bluemix-eu-de      
                      |__standard-eu-gb        deployment   databases-for-postgresql-standard:eu-gb      eu-gb                          bluemix-eu-gb      
                      |__standard-jp-tok       deployment   databases-for-postgresql-standard:jp-tok     jp-tok                         bluemix-jp-tok      
                      |__standard-osl01        deployment   databases-for-postgresql-standard:osl01      osl01                          bluemix-osl01      
                      |__standard-seo01        deployment   databases-for-postgresql-standard:seo01      seo01                          bluemix-seo01      
                      |__standard-us-east      deployment   databases-for-postgresql-standard:us-east    us-east                        bluemix-us-east      
                      |__standard-us-south     deployment   databases-for-postgresql-standard:us-south   us-south                       bluemix-us-south      
TSOTA@cloudshell:~$ 

4. サービスインスタンスを作成

前のステップで確認したサービスIDとプランIDを指定して、サービスインスタンスを作成します。
postgreSQL2という名前のサービスインスタンスを作成します。

サービスインスタンスを作成
TSOTA@cloudshell:~$ ibmcloud resource service-instance-create postgreSQL2 databases-for-postgresql standard jp-tok
Creating service instance postgreSQL2 in resource group Default of account ibmj-cloud-freeusage-p035 as [email protected]...
OK
Service instance postgreSQL2 was created.

Name:                postgreSQL2   
ID:                  xxxx 
GUID:                xxxx  
Location:            jp-tok   
State:               provisioning   
Type:                service_instance   
Sub Type:            Public   
Service Endpoints:   public   
Allow Cleanup:       false   
Locked:              false   
Created at:          2020-12-12T05:06:54Z   
Updated at:          2020-12-12T05:06:54Z   
Last Operation:                      
                     Status    create in progress      
                     Message   Started create instance operation      

サービスインスタンスをリストして、プロビジョニングのステータスを確認します。
ステータスがActiveになるまで待ちます。

サービスインスタンスのステータスを確認
TSOTA@cloudshell:~$ ibmcloud resource service-instances
Retrieving instances with type service_instance in resource group Default in all locations under account xxxx as xxxx...
OK
Name                              Location   State          Type   
・・・・
postgreSQL2                       jp-tok     provisioning   service_instance   
Retrieving instances with type service_instance in resource group Default in all locations under account xxxx as xxxx
OK
Name                              Location   State    Type   
・・・・
postgreSQL2                       jp-tok     active   service_instance   
TSOTA@cloudshell:~$ ibmcloud cdb ls
Retrieving instances for all database types in resource group Default in all locations under xxxx as xxxx...
OK
Name                          Location   State    
postgreSQL2                   jp-tok     active   

5. PostgreSQLのadminユーザーのパスワードを設定

adminユーザーのパスワードを設定します。

パスワードを設定
TSOTA@cloudshell:~$ ibmcloud cdb user-password postgreSQL2 admin
User Password>> 
Confirm User Password>> 
The user's password is being changed with this task:

Key                   Value   
ID                    xxxx   
Deployment ID         xxxx 
Description           Updating user.   
Created At            2020-12-12T05:21:46Z   
Status                running   
Progress Percentage   0   

Status                completed   
Progress Percentage   100   
Location              xxxx  
OK

6. adminユーザーで接続

ibmcloudコマンドでデータベースに接続してSQLやコマンドを実行できます。

データベースに接続
TSOTA@cloudshell:~$ ibmcloud cdb deployment-connections postgreSQL2 --start
Database Password>> 
psql (10.15 (Ubuntu 10.15-0ubuntu0.18.04.1), server 12.5)
WARNING: psql major version 10, server major version 12.
         Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

ibmclouddb=> select user;
 user  
-------
 admin
(1 row)

ibmclouddb=> create user user1 password 'isepassword';
CREATE ROLE
ibmclouddb=> create table t1(c1 int);
CREATE TABLE
ibmclouddb=> insert into t1 values (1);
INSERT 0 1
ibmclouddb=> select * from t1;
 c1 
----
  1
(1 row)

ibmclouddb=> \dt
 public | t1   | table | admin
ibmclouddb=> \q
TSOTA@cloudshell:~$ 

以上。