Administering PostgreSQL via pgAdmin
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
pgAdmin Installed: Ensure you have pgAdmin installed and running.
PostgreSQL Server: You should have access to a PostgreSQL server.
Steps
Step 1: Connecting to PostgreSQL Server
Open pgAdmin.
In the Browser panel, right-click on Servers and select Create > Server.
In the Create - Server dialog:
General Tab:
- Enter a name for the server (e.g.,
My PostgreSQL Server).
- Enter a name for the server (e.g.,
Connection Tab:
- Enter the Host, Port (default is
5432), Username, and Password.
- Enter the Host, Port (default is
Click Save to connect to the server.
Step 2: Creating a Database
In the Browser panel, right-click on the Databases node and select Create > Database.
In the Create - Database dialog:
Enter a name for the database (e.g.,
mydatabase).Optionally, you can set the owner and other parameters.
Click Save to create the database.
Step 3: Managing Users (Roles)
In the Browser panel, expand the server node and navigate to Login/Group Roles.
Right-click on Login/Group Roles and select Create > Login/Group Role.
In the Create - Role dialog:
General Tab:
- Enter a name for the role (e.g.,
dbuser).
- Enter a name for the role (e.g.,
Definition Tab:
- Set a password for the role.
Privileges Tab:
- Grant the necessary privileges (e.g.,
Can create database,Can login).
- Grant the necessary privileges (e.g.,
Click Save to create the role.
Step 4: Granting Privileges on a Database
In the Browser panel, expand the Databases node and select the database you created.
Right-click on the database and select Properties.
Go to the Privileges tab.
Click on the Add button to grant privileges to the user you created (e.g.,
dbuser).Select the privileges you want to grant (e.g.,
SELECT,INSERT,UPDATE, etc.).Click Save to apply the changes.
Step 5: Backing Up a Database
In the Browser panel, right-click on the database you want to back up and select Backup.
In the Backup Database dialog:
Set the Format (e.g.,
Plain,Custom,Tar).Choose a Filename and location for the backup file.
Click Backup to create the backup.
Step 6: Restoring a Database
In the Browser panel, right-click on the database you want to restore and select Restore.
In the Restore Database dialog:
Choose the Filename of the backup file.
Set the Format to match the backup format.
Click Restore to restore the database.
Step 7: Viewing Database Statistics
In the Browser panel, select the database.
Right-click on the database and select Dashboard.
View various statistics such as connections, active queries, and database size.
Step 8: Running SQL Queries
In the Browser panel, right-click on the database and select Query Tool.
In the query editor, you can run SQL commands, such as:
sql
Copy
SELECT * FROM your_table;Click the Execute/Refresh button (lightning bolt icon) to run the query.