Skip to main content

Command Palette

Search for a command to run...

Administering PostgreSQL via pgAdmin

Published
3 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

This tutorial will guide you through the basic administration tasks of PostgreSQL using pgAdmin. You will learn how to connect to a database, manage users, create databases, and perform backups and restores.

Prerequisites

  1. pgAdmin Installed: Ensure you have pgAdmin installed and running.

  2. PostgreSQL Server: You should have access to a PostgreSQL server.

Steps

Step 1: Connecting to PostgreSQL Server

  1. Open pgAdmin.

  2. In the Browser panel, right-click on Servers and select Create > Server.

  3. In the Create - Server dialog:

    • General Tab:

      • Enter a name for the server (e.g., My PostgreSQL Server).
    • Connection Tab:

      • Enter the Host, Port (default is 5432), Username, and Password.
  4. Click Save to connect to the server.

Step 2: Creating a Database

  1. In the Browser panel, right-click on the Databases node and select Create > Database.

  2. In the Create - Database dialog:

    • Enter a name for the database (e.g., mydatabase).

    • Optionally, you can set the owner and other parameters.

  3. Click Save to create the database.

Step 3: Managing Users (Roles)

  1. In the Browser panel, expand the server node and navigate to Login/Group Roles.

  2. Right-click on Login/Group Roles and select Create > Login/Group Role.

  3. In the Create - Role dialog:

    • General Tab:

      • Enter a name for the role (e.g., dbuser).
    • Definition Tab:

      • Set a password for the role.
    • Privileges Tab:

      • Grant the necessary privileges (e.g., Can create database, Can login).
  4. Click Save to create the role.

Step 4: Granting Privileges on a Database

  1. In the Browser panel, expand the Databases node and select the database you created.

  2. Right-click on the database and select Properties.

  3. Go to the Privileges tab.

  4. Click on the Add button to grant privileges to the user you created (e.g., dbuser).

  5. Select the privileges you want to grant (e.g., SELECT, INSERT, UPDATE, etc.).

  6. Click Save to apply the changes.

Step 5: Backing Up a Database

  1. In the Browser panel, right-click on the database you want to back up and select Backup.

  2. In the Backup Database dialog:

    • Set the Format (e.g., Plain, Custom, Tar).

    • Choose a Filename and location for the backup file.

  3. Click Backup to create the backup.

Step 6: Restoring a Database

  1. In the Browser panel, right-click on the database you want to restore and select Restore.

  2. In the Restore Database dialog:

    • Choose the Filename of the backup file.

    • Set the Format to match the backup format.

  3. Click Restore to restore the database.

Step 7: Viewing Database Statistics

  1. In the Browser panel, select the database.

  2. Right-click on the database and select Dashboard.

  3. View various statistics such as connections, active queries, and database size.

Step 8: Running SQL Queries

  1. In the Browser panel, right-click on the database and select Query Tool.

  2. In the query editor, you can run SQL commands, such as:

    sql

    Copy

     SELECT * FROM your_table;
    
  3. Click the Execute/Refresh button (lightning bolt icon) to run the query.

10 views