PostgreSQL (cluster)

Deploy PostgreSQL, an open source relational database platform designed for reliability, standards compliance, and scalability across many application types.

📘

Cluster deployment

The app is deployed with three Linode instances to create a highly available PostgreSQL cluster, each with the plan type and size you select.

Each of these Linode instances will appear on your invoice as separate items. To deploy PostgreSQL on a single instance, see the PostgreSQL guide.

1. Deploy PostgreSQL cluster

 Estimated deployment time: 5-10 minutes

Follow the deployment instructions from the Get started section to configure the app, deploy it, and verify software installation.

For information on the app-specific configurations, see the Configuration options section.

Base distribution and plan

  • Supported distribution: Ubuntu 24.04 LTS
  • Recommended plans: All plan types and sizes can be used.

Configuration options

Configure the required options to deploy your instance. For additional customization, add advanced options.

The table maps the Cloud Manager UI fields to their corresponding API/CLI keys (stackscript_data) required for automated deployments.

StackScript ID: 1068726

UI fieldAPI/CLI keyDescription
Required options
Linode API tokentoken_passwordA valid Linode API token with grants to deploy Linode instances. Follow Manage personal access tokens to get the token on your account with Read/Write access to Linodes.
Domain namecluster_nameThe cluster domain name used by the deployment to identify cluster resources.
Limited sudo usersudo_usernameYour preferred username for the limited sudo user entered without any capital letters, spaces, or special characters.
When adding a limited sudo user, the user is created with a strong generated password for your new Linode instance, and the account is assigned to the sudo group, which provides elevated permissions when running commands with the sudo prefix.
Note: For easier and more secure access with the sudo user, add an account SSH key for the Cloud Manager user during deployment and select that user as an authorized_user. Their SSH pubkey will be assigned to both the root and limited user.
Advanced options
Add Account SSH Keys to All Nodesadd_ssh_keysWhen set to yes, account SSH keys are added to the root user account (in the SSH Keys section) and to your limited users account on all deployed Linode instances. Possible values are yes or no. Defaults to yes.
PostgreSQL cluster sizecluster_sizeThe number of Linode instances created for this cluster. Value is fixed at 3.

Note: After the app deployment completes, the password for your limited sudo user is generated and stored in the .deployment-secrets.txt file in the home directory. Log in to your instance as root through the Lish console or SSH, then run cat /home/$USERNAME/.deployment-secrets.txt to view its contents.

Use API, CLI, or Terraform

In addition to deploying the app to a new Linode instance via Cloud Manager, you can also use the Linode API, CLI, or Terraform. When running the operation, you need to provide the StackScript ID, supported Linux distribution, and app-specific fields along with the standard Linode deployment configurations.

Note: Generate a personal access token to authenticate your API, CLI, or Terraform requests.

curl --location 'https://api.linode.com/v4/linode/instances' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer abc123def456hij789klm' \
  --data-raw '{
    "region": "us-east",
    "type": "g6-standard-2",
    "image": "linode/ubuntu24.04",
    "label": "my-postgresql-cluster-one-click-app",
    "root_pass": "@C0mpl3x#P@ssw0rd",
    "stackscript_id": 1068726,
    "stackscript_data": {
      "token_password": "abc123def456hij789klm",
      "cluster_name": "example-cluster",
      "sudo_username": "jsmith"
    }
  }'
linode-cli linodes create \
  --region us-east \
  --type g6-standard-2 \
  --label my-postgresql-cluster-one-click-app \
  --image linode/ubuntu24.04 \
  --root_pass @C0mpl3x#P@ssw0rd \
  --stackscript_id 1068726 \
  --stackscript_data '{"token_password":"abc123def456hij789klm", "cluster_name":"example-cluster", "sudo_username":"jsmith"}'
resource "linode_instance" "my-linode" {
  region         = "us-east"
  type           = "g6-standard-2"
  label          = "my-postgresql-cluster-one-click-app"
  image          = "linode/ubuntu24.04"
  root_pass      = "@C0mpl3x#P@ssw0rd"
  stackscript_id = 1068726
  stackscript_data = {
    "token_password" = "abc123def456hij789klm"
    "cluster_name"   = "example-cluster"
    "sudo_username"  = "jsmith"
  }
}

2. Use PostgreSQL

Modify Postgres users

By default, PostgreSQL creates a Linux user named postgres to access the database software.

To keep your database secure, don't use the postgres user for other purposes, like connecting to other networks.

  1. Change the postgres user’s Linux password.

    sudo passwd postgres
  2. Issue these commands to set a password for the postgres database user. Replace newpassword with a strong password and keep it in a secure place.

    su - postgres
    psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'newpassword';"

    This user is distinct from the postgres Linux user. The Linux user is used to access the database, and the PostgreSQL user is used to perform administrative tasks on the databases.

    The password set in this step is used to connect to the database via the network. Peer authentication is used by default for local connections.

Create a database

Run the commands as the postgres Linux user.

  1. Create a sample database called mytestdb.

    createdb mytestdb
  2. Connect to the test database.

    If the connection is successful, psql displays its version information and opens an interactive session.

    The mytestdb=# prompt indicates that you are connected to the mytestdb database and can enter SQL commands.

    psql mytestdb
    psql (12.2 (Debian 12.2-2.pgdg90+1))
    Type "help" for help.
    
    mytestdb=#

This is the PostgreSQL client shell, in which you can issue SQL commands. To see a list of available commands, use the \h command. You may find more information on a specific command by adding it after \h.

Create tables

This section contains examples that create a test database with an employee's first and last name, assigning each a unique key.

When creating your own tables, you may specify as many parameters (columns) as you need and name them appropriately.

Run the commands from the PostgreSQL client shell that you opened to create the mytestdb database.

  1. Create a table called employees in your test database.

    CREATE TABLE employees (employee_id int PRIMARY KEY, first_name varchar, last_name varchar);
  2. Insert a record into the table.

    INSERT INTO employees VALUES (1, 'John', 'Smith');
  3. View the contents of the employees table.

    The query returns all columns and rows in the employees table. In this example, the table contains one employee.

    SELECT * FROM employees;
    employee_id | first_name | last_name
    ------------+------------+-----------
              1 | John       | Smith
    (1 row)
  4. Exit the PostgreSQL shell by entering the \q command.

Create PostgreSQL roles

PostgreSQL grants database access through roles, which are used to specify privileges. Roles can be understood as having a similar function to Linux users.

In addition, roles may also be created as a set of other roles, similar to a Linux group.

PostgreSQL roles apply globally, so you don't need to create the same role twice if you want to grant it access to more than one database on the same server.

The example commands in this section should be run as the postgres Linux user.

  1. Add a new user role, then a password at the prompt.

    createuser examplerole --pwprompt

    If you need to delete a role, you can use the dropuser command in place of createuser.

  2. Connect to the database.

    psql mytestdb

    You'll be connected as the postgres database user by default.

  3. From the PostgreSQL shell, enter the following to grant all privileges on the table employees to the user examplerole:

    GRANT ALL ON employees TO examplerole;
  4. Exit the PostgreSQL shell by entering \q.

Additional resources

For more information about the installed packages, see their official documentation.

📘

Note that we can't vouch for the accuracy or timeliness of externally hosted resources.

For details on managing remote PostgreSQL servers, see our guide.


Did this page help you?