PostgreSQL Databases

Every Opalstack server includes a managed instance of PostgreSQL, a full-featured open-source object-relational database management system.

Your Opalstack dashboard lets you manage PostgreSQL databases and database users.

Adding PostgreSQL Databases

  1. Click PostgreSQL in the dashboard sidebar.

  2. Click the green "Create PostgreSQL" button in the upper right corner of the page. A form will appear.

  3. Enter the desired name for your new database.

  4. Select the server for your new database.

  5. Select the desired character set for your database.

  6. Select the database users that will be allowed to access the database.

  7. When all inputs are valid a green "Create PostgreSQL"button will apear on the form. Click the button to save your new database.

Deleting PostgreSQL Databases

  1. Click PostgreSQL in the dashboard sidebar.

  2. Select the database (or databases) to be deleted by checking its checkbox in the database list in the top half of the page. A red Delete PostgreSQL button will appear above the list.

  3. Click the red "Delete PostgreSQL" button above the database list. A confirmation prompt will appear.

  4. Click the red "Delete PostgreSQL" button in the confirmation prompt.

  5. After you confirm the database items will turn red while the operation is pending and will disappear a few seconds later when the operation is complete.

Adding PostgreSQL Users

  1. Click on PostgreSQL in the dashboard sidebar.

  2. Click the green Create PostgreSQL User button near the top-right corner of the page. A form will appear.

  3. Enter a name for your PostgreSQL user in the form.

  4. Select the server where the new PostgreSQL user will be created. This must be the same server as the database that the user will connect to.

  5. Enable the External Access option if you need to be able to connect to the database directly from your own computer or other hosts.

  6. Click the green Create PostgreSQL User button on the form to save your new PostgreSQL user.

  7. The new PostgreSQL user will be ready after a few seconds. You can retrieve the initial database user password from your dashboard notice log.

Changing PostgreSQL User Passwords

  1. Click on PostgreSQL in the dashboard sidebar. Your existing PostgreSQL users are listed in the lower half of the page.

  2. Click the checkbox next to the PostgreSQL user (or users) that you want to modify. A row of buttons will appear above the list.

  3. Click the purple "Change PostgreSQL User Passwords" button. A form will appear.

  4. Enter the new database user password in the field labeled "New Password".

    Passwords must meet the following requirements:

    • Must contain at least 1 of each of the following:
    • 1 lowercase letter A-Z
    • 1 uppercase letter a-z
    • 1 numeral 0-9
    • 1 special character
    • Must be a minimum of 8 and a maximum of 32 characters in length.
  5. Enter your new password again in the field labeled "Verify Password".

    When you've entered identical valid passwords in both fields a purple "Change Password" button will appear.

  6. Click the "Change Password" button to save your new password. The password will be applied to all of the database users that were selected in step 2 above.

Deleting PostgreSQL Users

  1. Click on PostgreSQL in the dashboard sidebar. Your existing PostgreSQL users are listed in the lower half of the page.

  2. Click the checkbox next to the PostgreSQL user (or users) that you want to delete. A row of buttons will appear above the list.

  3. Click the red "Delete PostgreSQL Users" button. A confirmation form will appear.

  4. Click the red button to confirm the operation, or click the cancel button to cancel it.

  5. After you delete the PostgreSQL user its status will update while the operation is pending, and the user will disappear a few seconds later when the operation is complete.

Managing PostgreSQL user access

You can control access to your PostgreSQL databases by editing the database in your dashboard.

  1. Click on PostgreSQL in the dashboard sidebar.

  2. Click the pencil icon for the database that you want to modify. A form will appear.

  3. Use the form to assign your PostgreSQL users as needed. Read/write users can view and modify data in your database, while read-only users can only view it.

  4. Click the green "Update PostgreSQL" button to save your changes.

  5. The database item will turn purple while the change is pending and then back to dark blue when the change is complete.

Managing external access

  1. Click on PostgreSQL in the dashboard sidebar. Your existing PostgreSQL users are listed in the lower half of the page.

  2. Click the checkbox next to the PostgreSQL user (or users) that you want to delete. A row of buttons will appear above the list.

  3. Click the pink "Toggle PostgreSQL External Access" button. The external access setting will be immediately toggled.

Connecting to PostgreSQL Databases

This section shows how to connect to your PostgreSQL databases on Opalstack from clients and applications that use PostgreSQL databases.

Connection Parameters

The parameters used to connect to a PostgreSQL database on your Opalstack server are:

  • Host: localhost
  • Port: 5432
  • Database name: your PostgreSQL database name
  • Database user name: your PostgreSQL database user name
  • Database user password: your PostgreSQL database user password

ℹ️ Note

that many applications use a default configuration with the host as localhost and the port as 5432 as mentioned above so those parameters might be optional or not present in your application settings. Consult your application's documentation for its specific configuration requirements.

Connection Examples

The following examples use mydb as the PostgreSQL database name, mydbuser as the database user name, and mydbpassword as the database user password. Replace them with your own database name and credentials.

Command line client

First log in to a SSH session as a shell user on your server, then...

psql -U mydbuser mydb

...and enter your database user password when prompted.

Python

First install the psycopg2 library in your environment...

export PATH=/usr/pgsql-11/bin/:$PATH
cd ~/apps/appname
source env/bin/activate
pip install psycopg2

... then in your Python code:

import psycopg2
conn = psycopg2.connect(host='localhost', user='mydbuser',
                     password='mydbpassword', dbname='mydb')

PHP

In your PHP code:

$conn = pg_connect("host=localhost dbname=mydb user=mydbuser password=mydbpassword")

Javascript

First install the pg package into your Node.js project directory...

export PATH=/usr/pgsql-11/bin/:$PATH
npm install pg

... then in your Javascript code:

var pg = require('pg');
var conn = new pg.Client({
  user: 'mydbuser',
  host: 'localhost',
  database: 'mydb',
  password: 'mydbpassword',
  port: 5432,
})
conn.connect()

Ruby

First install the pg gem...

export PATH=/usr/pgsql-11/bin/:$PATH
gem install pg

... then in your Ruby code:

require 'pg'
conn = PGconn.connect(:host => 'localhost', :port => 5432, :dbname => 'mydb',
                      :user => 'mydbuser', :password => 'mydbpassword' )

Connecting to a PostgreSQL Database Remotely via a SSH Tunnel

Your Opalstack dashboard gives you the option to enable external access for your PostgreSQL database users, but for additional security we recommend that you make remote connections through a SSH tunnel.

  1. Create a PostgreSQL database and user.

  2. Create a shell user.

  3. On the client machine making the remote connection (for example, your own computer) open your terminal application and run the following command to establish the tunnel, replacing myuser@opalN.opalstack.com with your shell username and Opalstack server hostname:

    ssh myuser@opalN.opalstack.com -L 5432:127.0.0.1:5432 -N
    

    ℹ️ Note

    • If you have a PostgreSQL server already running on the client machine, then you will need to change the local port used by the tunnel. The following example uses port 5433 as the local port: ssh myuser@opalN.opalstack.com -L 5433:127.0.0.1:5432 -N
    • If you're using PuTTY for your SSH connections, use 5432 as the source port and 127.0.0.1:5432 as the destination under "Category > Connection > SSH > Tunnels".
  4. Finally, on the client machine configure your application to connect to your database with the following parameters:

    • Host: 127.0.0.1
    • Port: 5432 (or whatever your local port is)
    • Database name: your PostgreSQL database name
    • Database user name: your PostgreSQL database user name
    • Database user password: your PostgreSQL database user password

Connecting to a PostgreSQL Database over the Web

Your Opalstack service includes a web-based database administration tool called Adminer which is similar to phpPgAdmin.

To access Adminer:

  1. Go to https://SERVERNAME.opalstacked.com/adminer/ in your browser, replacing SERVERNAME with your Opalstack server name (for example "opal1" or "vps2").

  2. Complete the login form as follows:

    • Server: PostgreSQL
    • Username: your PostgreSQL user name
    • Password: your PostgreSQL user password
    • Database: your PostgreSQL database name.
  3. Click the Login button to log in to your database with the credentials you provided in step 2.

Exporting and Importing PostgreSQL Databases

This section shows how to export data from, and import data to, your PostgreSQL databases on Opalstack. The examples use mydb as the database name, mydbuser as the database username, and mydb.sql as the filename for the imported/exported data - be sure to replace those values with the names of your own database and database user.

Exporting PostgreSQL Databases

You can export a PostgreSQL database by executing the following command in a SSH session on your Opalstack server:

pg_dump -U mydbuser -f mydb.sql mydb

Enter your PostgreSQL database user password when prompted.

When the command is complete, the exported data will be stored in the file mydb.sql in your SSH session's current working directory.

Importing PostgreSQL Databases

You can import a PostgreSQL database by executing the following command in a SSH session on your Opalstack server:

psql -U mydbuser mydb < mydb.sql

Enter your PostgreSQL database user password when prompted.

When the command is complete, the imported data will be stored in your mydb PostgreSQL database.

Scheduled Backups for PostgreSQL Databases

This section shows how to create scheduled backups of a PostgreSQL database on Opalstack.

The examples use mydb as the database name, mydbuser as the database user name, and mydbpassword as the database user password - be sure to replace those values with the names of your own database, user, and password.

  1. Log in to a SSH session on your Opalstack server.

  2. Create the files and directories used by your scheduled PostgreSQL backups:

    export DBNAME=mydb
    mkdir -m 700 -p ~/.local/bin
    mkdir -m 700 -p ~/.local/etc/psql_backups
    mkdir -m 700 -p ~/backups/psql
    touch ~/.pgpass
    chmod 600 ~/.pgpass
    touch ~/.local/bin/backup_$DBNAME
    chmod 700 ~/.local/bin/backup_$DBNAME
    
  3. Edit the database credentials file named ~/.pgpass and add the following line, replacing mydbuser and mydbpassword with your database username and password.

    localhost:5432:mydb:mydbuser:mydbpassword
    
  4. Edit the script ~/.local/bin/backup_mydb and add the following lines that will run your backup:

    #!/bin/bash
    
    export DBNAME=mydb
    export DBUSER=mydbuser
    
    /bin/pg_dump -b -Fp -U $DBUSER $DBNAME \
        > $HOME/backups/psql/$DBNAME-$(date +\%Y\%m\%d\%H\%M).sql \
        2>> $HOME/backups/psql/$DBNAME.log
    
  5. Run crontab -e and create a crontab entry to schedule the backup. The following example will schedule the backup to run daily at 3:30 AM UTC (server time):

    30 3 * * * $HOME/.local/bin/backup_mydb
    

    Please see the following pages for general help with cron jobs:

Once the above steps are complete, your backup will run at the schedule time to create a backup of your PostgreSQL database in ~/backups/psql on the server.