How to work with pgAdmin in PostgreSQL
By: Aymen Khan
Tags: Database, Postgresql

In this blog post, we will learn what pgAdmin is, how to use it to connect to PostgreSQL and create a new database.

What is pgAdmin?

pgAdmin is the leading open source management tool for PostgreSQL with a powerful graphical interface that simplifies the creation, maintenance and use of database objects. It supports all PostgreSQL features, from writing simple SQL queries to developing complex databases. It’s designed to query an active database in real-time, which allows you to stay current with new modifications in your database.

Connect to PostgreSQL with pgAdmin

In my previous blog post, I went through the steps of installing PostgreSQL onto your computer.

Once you have followed those steps and have PostgreSQL installed, you will be able to access pgAdmin by clicking the Windows icon in the taskbar and searching for the pgAdmin 4 (the version at the time of download) application and clicking on it to launch it.

The first time you launch pgAdmin, you will be asked to set a master password, which is used to secure and later unlock the saved server passwords. When pgAdmin opens, the interface features a blue menu bar at the top and a window divided into two panes: the tree view on the left, and a tabbed browser on the right.

Create a new database

To create a database, you must be a database superuser or have the CREATE privilege. The database user that was created during the installation, postgres, is a superuser, so we can use this one.

On the left pane, select the name of the server you want your new database to be on and right-click on it. Go to the Create option and then click on Database.

This will launch the Database dialog, which organizes the development of a database through the following dialog tabs: General, Definition, Security, and Parameters, and SQL.

A name for the database is specified in the Database field, the owner of the database is selected from the drop-down list in the Owner field, and notes about the database are stored in the Comment field.

Click the Definition tab to set properties for the new database.

Select a character set from the drop-down list in the Encoding field. The default is UTF8.

Select the database template from which you want to create the new database from the drop-down list in the Template field. If no template is specified, the database will automatically use template1.

Select a tablespace name from the drop-down list in the Tablespace field. The selected tablespace will be the default tablespace used to contain database objects. The default is the template database’s tablespace.

Select the collation order from the drop-down list in the Collation field. Collation specifies the sort order of strings that affect the result of the ORDER BY clause in the SELECT statement. The template database’s collation is the default collation for the new database.

Select the character classification from the drop-down list in the Character type field. This affects the categorization of characters, e.g. lower, upper and digit. The default, or a blank field, uses the character classification of the template database.

Specify a connection limit in the Connection limit field to configure the maximum number of connection requests to the database. The default value of -1 allows unlimited connections to the database.

Click the Security tab to assign privileges and define security labels.

You can use the Privileges panel to assign privileges to a user/role. Click the Add icon (+) to add these privileges:

Select the name of the user/role from the drop-down list in the Grantee field.

Click inside the Privileges field and check off the boxes to the left of the privileges to be to granted to the specified user/role.

The Grantor field is populated with the current user, who is the default grantor for granting the privilege(s).

You can add more privileges by clicking the add button and repeating the steps for adding these. You can also delete privileges by clicking on the trash icon on the left of each row.

You can also use the Security labels panel to define security labels applied to the database. Click the Add icon (+) to add these labels:

Specify a security label provider in the Provider field. This must be loaded and consent to the proposed labeling operation.

Specify a security label in the Security label field.

You can add more security labels by clicking the add button and repeating the steps for adding these. You can also delete security labels by clicking on the trash icon on the left of each row.

Click the Parameters tab to set parameters for the database.

Click the Add (+) icon to add each parameter for the database:

Select a parameter from the drop-down list in the Name field

Specify a value for the parameter in the Value field

Select the role to which the parameter setting specified will apply from the drop-down list in the Role field

You can add more parameters by clicking the add button and repeating the steps for adding these. You can also delete parameters by clicking on the trash icon on the left of each row.

Click the SQL tab to continue.

All of the entries in the Database dialog generate a SQL command that we can see here. If needed, you can revisit any of the previous tabs to make changes to the SQL code. If everything looks right, you can hit Save and this will create the new database.

Sources