To connect your PostgreSQL database to Magento BI via an SSH tunnel, you (or your team, if you're not a techie) will need to do a few things:
- Retrieve the Magento BI public key
- Whitelist the Magento BI IP address
- Create a Linux user for Magento BI
- Create a Postgres user for Magento BI
- Enter the connection and user info into Magento BI
It's not as complicated as it might sound. Let's get started.
Retrieving the Magento BI public key
The public key is used to authorize the Magento BI Linux user. In the next section, we'll create the user and import the key.
- Go to Manage Data > Connections and click the Add a Data Source button.
- Click the PostgreSQL icon.
- After the PostgreSQL credentials page opens, toggle the Encrypted button to Yes. This will display the SSH setup form.
- The public key is located underneath this form.
Leave this page open throughout the tutorial - you'll need it in the next section and at the end.
If you're a bit lost, here's how to navigate through Magento BI to retrieve the key:
Whitelisting the Magento BI IP address
For the connection to be successful, your must configure your firewall to allow access from our IP address. It's 220.127.116.11/32, but it's also on the PostgreSQL credentials page. See the blue box in the GIF above? That's it!
Creating a Linux user for Magento BI
This can be a production or slave machine, as long as it contains real-time (or frequently updated) data. You may restrict this user any way you like, as long as it retains the right to connect to the PostgreSQL server.
- To add the new user, run the following commands as root on your Linux server:
adduser rjmetric -p<password> mkdir /home/rjmetric mkdir /home/rjmetric/.ssh
- Remember the public key we retrieved in the first section? To ensure the user has access to the database, we need to import the key into authorized_keys.
Copy the entire key into the authorized_keys file as follows:
touch /home/rjmetric/.ssh/authorized_keys "<PASTE KEY HERE>" >> /home/rjmetric/.ssh/authorized_keys
- To finish creating the user, alter the permissions on the /home/rjmetric directory to allow access via SSH:
chown -R rjmetric:rjmetric /home/rjmetric chmod -R 700 /home/rjmetric/.ssh
If the sshd_config file associated with the server is not set to the default option, only certain users will have server access - this will prevent a successful connection to Magento BI. In these cases, it's necessary to run a command like AllowUsers to allow the rjmetric user access to the server.
Creating an Magento BI Postgres user
Your organization may require a different process, but the simplest way to create this user is to execute the following query when logged into Postgres as a user with the right to grant privileges. The user should also own the schema that Magento BI is being granted access to.
GRANT CONNECT ON DATABASE <database name> TO rjmetric WITH PASSWORD <secure password>;
GRANT USAGE ON SCHEMA <schema name> TO rjmetric;
GRANT SELECT ON ALL TABLES IN SCHEMA <schema name> TO rjmetric;
ALTER DEFAULT PRIVILEGES IN SCHEMA <schema name> GRANT SELECT ON TABLES TO rjmetric;
Replace <secure password> with a secure password, which can be different than the SSH password. Additionally, make sure you replace <database name> and <schema name> with the appropriate names in your database.
If you want to connect multiple databases or schemas, repeat this process as necessary.
Entering the connection and user info into Magento BI
To wrap things up, we need to enter the connection and user info into Magento BI. Did you leave the PostgreSQL credentials page open? If not, go to Manage Data > Connections and click the Add a Data Source button, then the PostgreSQL icon. Don't forget to toggle the Encrypted button to Yes.
Enter the following info into this page, starting with the Database Connection section:
- Username: The RJMetrics Postgres username (should be rjmetric)
- Password: The RJMetrics Postgres password
- Port: PostgreSQL port on your server (5432 by default)
- Host: 127.0.0.1
Under "SSH Connection":
- Remote Address: The IP address or hostname of the server we will SSH into
- Username: Our SSH login name (should be rjmetric)
- SSH Port: SSH port on your server (22 by default)
That's it! When you're finished, click the Save & Test button to complete the setup.