SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Create a User, Tables and Import Data Using SAP HANA HDBSQL

Use the command line tool HDBSQL to connect to a SAP HANA database, create a user, and create tables which will be used in subsequent tutorials in this mission.

Overview

🎓 beginner 15 min. SAP HANA CloudBeginnerSAP HANA Cloud SAP HANA DatabaseSAP HANASAP HANA Express EditionSql

You will learn

  • โœ”How to connect to SAP HANA from a client machine using HDBSQL
  • โœ”How to create a user, schema, tables and import data

Prerequisites

Prerequisites

  • You have completed the first 2 tutorials in this mission.

Steps

Intro

HDBSQL is used in this tutorial as it is part of the SAP HANA client install. HDBSQL is a basic tool for executing SQL scripts and providing an interface for interactive queries. Another option to execute SQL operations is the SQL Console that is part of SAP HANA Cloud Central or the SAP HANA database explorer which is part of an on-premise install of the SAP HANA Cockpit or a server + applications SAP HANA, express edition install.


Step 1 Connect to SAP HANA using hdbsql
โ€”

This step demonstrates how to connect to a SAP HANA instance using HDBSQL from the SAP HANA client installation.

  1. Copy and paste the following command to see the command line options and press the space bar to advance though the information.

    Shell
    hdbsql -h | more
  2. Connect to either SAP HANA Cloud or SAP HANA, express edition using host, port, and credentials. Details on where to find these values are covered in the third and fifth steps of the first tutorial in this mission.

    • To connect to SAP HANA Cloud, see the following general command.

      Shell
      hdbsql -n <host name>:<port> -u <user> -p <password>

      The following is a connection example for the SAP HANA Cloud. Notice that if the -p is not provided then you will be prompted to enter your password.

      Shell
      hdbsql -n 3b2gf55e-4214-4bd9-adfc-f547d8e2d384.hana.trial-us10.hanacloud.ondemand.com:443 -u DBADMIN

      Connect with hdbsql
      Connect with hdbsql

    • For SAP HANA, express edition, there are two options shown below to connect.

      Shell
      hdbsql -n <ip address>:39015 -u <user> -p <password>
      or
      hdbsql -n <ip address> -d <database name> -i <instance number> -u <user> -p <password>

      Below are two connection examples for SAP HANA, express edition.

      Shell
      hdbsql -n <ip address>:39015 -u SYSTEM -p your_password
      or
      hdbsql -n <ip address> -d HXE -i 90 -u SYSTEM -p your_password

      Notice that either the host and port combination or the host, database name and instance number can be used to connect.

  3. If your connection succeeded, proceed to sub-step 4. If not, the following information may be helpful in diagnosing a failed connection.

    • The HANA Cloud instance can be configured to enable applications running from outside the SAP BTP to connect. The current setting is shown in SAP HANA Cloud Central in the screenshot below.

      screenshot showing the allowlist
      screenshot showing the allowlist

      An example of configuring this setting is shown in Allow connections to SAP HANA Cloud instance from selected IP addresses โ€” using the command line.

    • The SAP HANA Cloud, HANA database free tier instance will be automatically stopped overnight. That means you need to restart your instance before working with it each new day.

    • Connections to a HANA Cloud instance must use encryption. The default encryption library on Windows is mscrypto and on Linux and macOS it is OpenSSL. The following example demonstrates how one could use the SAP provided conmmoncrypto library instead of the default encryption library. Note, the following steps require that the SAP HANA Client be downloaded from the SAP Software Downloads as the download includes the SAP Common Crypto library (libsapcrypto). Note that the environment variables can also be set by running source hdbclienv.sh or hdbclienv.bat.

      Shell
      mkdir ~/.ssl
      # Download the public root certificate used by HANA Cloud
      wget --no-check-certificate https://cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem -O ~/.ssl/DigiCertGlobalRootCA.crt.pem
      # Show the command help for the sapgenpse
      sapgenpse -h
      # SECDIR & LD_LIBRARY_PATH environment variables are required when using the commoncrypto library
      export SECUDIR=~/sap/hdbclient
      export LD_LIBRARY_PATH=~/sap/hdbclient:$LD_LIBRARY_PATH
      # macOS only
      export DYLD_LIBRARY_PATH=~/sap/hdbclient
      # Create a PSE (Personal Security Environment) which will be used to contain the public root certificate of SAP HANA Cloud.  
      # Press enter twice to not provide a pin
      sapgenpse gen_verify_pse -p "$SECUDIR/sapcli.pse"
      Shell
      # Add the certificate to the PSE
      sapgenpse maintain_pk -p "$SECUDIR/sapcli.pse" -a ~/.ssl/DigiCertGlobalRootCA.crt.pem
      # View the contents of the PSE
      sapgenpse maintain_pk -p "$SECUDIR/sapcli.pse" -l
      # Connect using the SAP commoncrypto library rather than OpenSSL. Replace the host, user, and password values.
      hdbsql -sslprovider commoncrypto -n 3b2gf55e-4214-4bd9-adfc-f547d8e2d384.hana.trial-us10.hanacloud.ondemand.com:443 -u <USER> -p <Password>
      Shell
      REM In a browser download https://cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem
      REM Show the command help for the sapgenpse
      sapgenpse -h
      REM SECDIR environment variable is required when using the commoncrypto library
      set SECUDIR=C:\SAP\hdbclient
      REM Create a PSE (Personal Security Environment) which will be used to contain the public root certificate of SAP HANA Cloud.  
      REM Press enter twice to not provide a pin
      sapgenpse gen_verify_pse -p "%SECUDIR%/sapcli.pse"
      REM if the above command fails, try using the -log option for additional output.
      Shell
      REM Add the certificate to the PSE
      sapgenpse maintain_pk -p "%SECUDIR%/sapcli.pse" -a %USERPROFILE%/Downloads/DigiCertGlobalRootCA.crt.pem
      REM View the contents of the PSE
      sapgenpse maintain_pk -p "%SECUDIR%/sapcli.pse" -l
      REM Connect using the SAP commoncrypto library rather than OpenSSL. Replace the host, user, and password values.
      hdbsql -sslprovider commoncrypto -n 3b2gf55e-4214-4bd9-adfc-f547d8e2d384.hana.trial-us10.hanacloud.ondemand.com:443 -u <USER> -p <Password>

      For additional details see Server Certificate Authentication.

    • If you are on a Linux or Mac machine and the hdbsql connection fails with the error message below, it indicates that the OpenSSL library could not locate a trust store in the default location.

      Cannot create SSL context: SSL trust store cannot be found: /Users/user1/.ssl/trust.pem

      A public root certificate to validate the server’s certificate is needed. More information about the required DigiCert root certificate can be found at Secure Communication Between SAP HANA Cloud and JDBC/ODBC Clients and 3397584 - HANA Cloud Connections will switch from “DigiCert Global Root CA” to “DigiCert TLS RSA4096 Root G5”.

      It can be downloaded from Download PEM, renamed to trust.pem and saved to the specified location. For further details, see Secure connection from HDBSQL to SAP HANA Cloud.

  4. Type \s for status information

    Shell
    \s

    Notice that for the SAP HANA Cloud instance, the version number is 4.x.

    Connect to HANA Cloud
    Connect to HANA Cloud

    Notice that for the HANA, express edition, the version number is 2.x.

    Connect to HXE
    Connect to HXE

Step 2 Create user and schema
+
Step 3 Store connection details in secure user store (hdbuserstore)
+
Step 4 Create tables and insert data
+
Step 5 Interactive, non-interactive, substitution variables, and prepared statements
+
Step 6 Knowledge check
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 6
1. Connect to SAP HANA using hdbsql 2. Create user and schema 3. Store connection details in secure user store (hdbuserstore) 4. Create tables and insert data 5. Interactive, non-interactive, substitution variables, and prepared statements 6. Knowledge check

Learn more →