This topic describes how to create an external store to associate Simple Log Service (SLS) with a PostgreSQL database.
Prerequisites
You have collected data in SLS. For more information, see Data collection.
You have installed the SLS CLI and configured the service endpoint and AccessKey pair.
You have stored data in an Alibaba Cloud RDS for PostgreSQL database, an Alibaba Cloud ADB for PostgreSQL database, or an Alibaba Cloud Hologres instance.
The PostgreSQL database is in an Alibaba Cloud virtual private cloud (VPC). The RDS instance, ADB instance, or Hologres instance must be in the same region as the SLS project.
ImportantYou cannot connect to a PostgreSQL database using an Internet endpoint.
Background information
The external store feature of SLS lets you associate SLS with an Alibaba Cloud RDS for PostgreSQL database, an Alibaba Cloud ADB for PostgreSQL database, or an Alibaba Cloud Hologres instance. You can also write query and analysis results to a PostgreSQL database for further processing.
Procedure
1. Configure a whitelist
RDS for PostgreSQL database
Add the following CIDR blocks to the whitelist: 100.104.0.0/16, 11.194.0.0/16, and 11.201.0.0/16. For more information, see Set a whitelist.
Alibaba Cloud Hologres
Add the following CIDR blocks to the whitelist: 100.104.0.0/16, 11.194.0.0/16, and 11.201.0.0/16. For more information, see IP whitelist.
ADB for PostgreSQL database
Add the following CIDR blocks to the whitelist: 100.104.0.0/16, 11.194.0.0/16, and 11.201.0.0/16. For more information, see Configure a whitelist.
2. Create an ExternalStore
Log on to the server where the CLI is installed. On the command line, run the
touchcommand to create a configuration file named /home/shell/config.json. Add the following script to the config.json file. You must replace the values of theregion,vpc-id,host,port,username,password,db, andtableparameters with your actual values.Parameter description
Example
{ "externalStoreName":"sls_join_meta_store", "storeType":"postgresql", "parameter":{ "region":"cn-qingdao", "vpc-id":"vpc-m5eq4irc1pucp*******", "host":"localhost", "port":"3306", "username":"user", "password":"****", "db":"scmc", "table":"join_meta" } }externalStoreNameThe name of the ExternalStore. The name must be in lowercase.
storeTypeThe type of the data source. Set the value to postgresql.
regionThe region, such as cn-wulanchabu or cn-hangzhou.
ImportantThe RDS instance, Hologres instance, or ADB instance must be in the same region as the SLS project.
vpc-idThe ID of the VPC where the database is located. This parameter varies based on the database type:
If your RDS for PostgreSQL instance is in a VPC, set vpc-id to the ID of that VPC.
If your ADB for PostgreSQL database is in a VPC, set vpc-id to the ID of the VPC that contains the instance.
If you use an Alibaba Cloud Hologres database in a VPC, set vpc-id to the ID of the Hologres instance's VPC.
hostThe internal endpoint of the database.
portThe port number of the database instance.
usernameThe username of the database.
passwordThe password of the database.
dbThe name of the database.
tableThe name of the database table. The following formats are supported:
`table_name`, for example, `test`.
`schema_name.table_name`, for example, `public.test`.
Run the following command on the command line to create an ExternalStore. In the command, replace project_name with the name of your SLS project. This topic uses
log-rds-demoas an example.aliyunlog log create_external_store --project_name="log-rds-demo" --config="file:///home/shell/config.json"Query the information about the ExternalStore. If the command runs successfully, no response is returned. Run the
aliyunlog log get_external_store --project_name="log-rds-demo" --store_name="sls_join_meta_store" --format-output=jsoncommand to query the details of the external data source. The following information is returned:{ "externalStoreName": "sls_join_meta_store", "parameter": { "db": "postgres", "host": "rm-bp1******rm76.pg.rds.aliyuncs.com", "port": "5432", "region": "cn-wulanchabu", "table": "test", "timezone": "", "username": "user", "vpc-id": "vpc-m5eq4irc1pucp*******" }, "storeType": "postgresql" }
3. Query the associated PostgreSQL data
After you create the external store, go to the SLS console. In the project named log-rds-demo, click any logstore and run the * | select * from sls_join_meta_store query statement to query the associated PostgreSQL data.

Related operations
Update the PostgreSQL external store.
aliyunlog log update_external_store --project_name="log-rds-demo" --config="file:///home/shell/config.json"Delete the PostgreSQL external store.
aliyunlog log delete_external_store --project_name="log-rds-demo" --store_name=sls_join_meta_store