5. Use pgAdmin4 to graphically create and manage PostgreSQL databases

Through the previous articles, we explained how to install the PostgreSQL database software and the pgAdmin4 graphical management tool.

Today we continue to learn how to graphically create and manage PostgreSQL databases through the pgAdmin4 management tool.

1. The basic working method of PostgreSQL
Before learning how to use PostgreSQL to create a database, we need to understand how it works. Understanding the basic operation mode of PostgreSQL can help us better understand the knowledge to be introduced later.

Simply put, PostgreSQL uses a client/server (C/S) model. This means that in PostgreSQL, there are two main roles:

  1. Server (also called "backend"): This is a program that manages database files, accepts connection requests from client programs, and performs operations specified by the client. The name of this server program is "postgres".

  2. Client (also called "front-end") programs: These are the tools that users use to tell the database what to do. Client programs can be various, such as text tools, graphics programs, Web servers for displaying web pages, or specialized database maintenance tools. Some client programs are provided with PostgreSQL, while most are developed by users themselves.

Just like when we use a computer to browse JD.com, our computer is actually communicating with the server on the Internet. The PostgreSQL client and server can run on different computers. They communicate via TCP/IP networks. One thing to remember is that files that are accessible on the client computer may not necessarily be accessible on the server computer, or their location and name may be different.

PostgreSQL server can handle connections from multiple clients simultaneously. To achieve this, the server creates a new process for each connection. In this way, the client and the new process can communicate directly without worrying about being disturbed by the original postgres process. Therefore, the main server process will always be running, waiting for the client's connection request; in contrast, the client and its associated server process will constantly appear and disappear.

2. Create database

To check if the client can connect to the database server, we can first try to create a new database on the server where PostgreSQL is installed. The PostgreSQL server can manage one or more databases at the same time. Generally, each user will create and use independent databases for different projects.

It would be better if your system administrator has already created the database for you and told you its name and connection information.

In order to create a database, you must first have an installed PostgreSQL service running on the server. Each instance running a PostgreSQL server manages one or more databases.

1) Create database graphically

Next, we open pgAdmin4 installed on the computer.
Insert image description here
1. Modify the interface language

If necessary, you can first change the interface language to Simplified Chinese to facilitate subsequent use.

Click the menu [File]-[Preferences] to open the preferences.
Insert image description here
Find [User Language] on the left, click the drop-down box on the right, select [Simplified Chinese], and then click [Save] to save.

Insert image description here

A prompt box will pop up, indicating that pgAdmin4 needs to be reloaded. Click [Refresh] to refresh. pgAdmin4
After reloading pgAdmin4, the interface has been changed to Simplified Chinese.
Insert image description here

2. Connect to the PostgreSQL database server.

Click the menu [Object] – [Registration] – [Server].
Insert image description here
Enter various connection information of the PostgreSQL database server in the interface.
[General]–[Name]: Give the connection a name that is easy to remember and distinguish, such as: PG-Database
Insert image description here
Enter the PostgreSQL database on the [Connection] page The server's IP address and password, if not using the default port [5432], also need to be changed to the configured connection port.

Insert image description here
Click Save. At this time, pgAdmin4 will use the configured information to try to connect to the PostgreSQL database server.
After the connection is successful, the following interface will appear:

Insert image description here

If the connection is unsuccessful, you need to check whether the entered server IP address and password are correct, and whether the firewall settings of the client computer and server are correct.

3. Create a new database

Right-click [Database] – [Create] – [Database].
Insert image description here
Configure various information of the new database in the pop-up [Create Database] window.
[Database]: The name of the created database, must be filled in.
[OID]: Can be ignored in new versions of PostgreSQL.
[Owner]: The default is the postgres system user. Just keep the default during the learning stage.
[Comment]: You can comment on the purpose of the created database.
Insert image description here

Supplementary knowledge:
[OID]: In the PostgreSQL database, OID (Object
Identifier, object identifier) ​​is a unique An integer identifier that identifies an object in a database (usually a row in a table). OIDs were used in the past to uniquely identify rows in a table, but starting with PostgreSQL version 12, the OID column is no longer used by default as a hidden column of the table. The OID column is no longer automatically created, so we won't see it when creating the table.

If we see an option or setting involving OID when creating a PostgreSQL database using pgAdmin 4, that is because we can choose whether we want to enable or disable the OID column in a specific table.
In new versions of PostgreSQL, the use of OID is no longer required, and it is no longer the default behavior. Most users no longer use OID columns because it adds storage and maintenance overhead and is often unnecessary.

If we are not sure whether we need to enable the OID column, we usually don't need to worry about it. In pgAdmin4 we can choose whether to include the OID column when creating the table, but if you don't explicitly need it it is recommended to leave it disabled.
If you need to use the OID column for a specific purpose, you can enable it. Be aware, however, that OID columns need to be handled with care to avoid potential performance and management issues.

In the [Definition] page, we can set the database parameters such as the character set encoding, template, table space and connection limit of the database. At the beginning stage, we can not choose it and just use the system default.

[Connection limit]: The maximum number of connections is limited. The default is -1, which means unlimited connections. If you need to limit the number of connections, you can enter a positive integer, such as [300].

Insert image description here
The [Security] page is where grant authorization commands are visually completed and is generally not used.

In our real work scenario, database permissions are set and managed uniformly by the company's database administrator [DBA], and developers generally cannot set them.

For learning and demonstration purposes here, we use the postgres account to authorize [ALL] the public account.

Insert image description here

In the [Parameters] page, you can set specific database parameters.

Insert image description here
Here, we set the value of a parameter "application_name" to "Default". Of course, it does not need to be set.

Supplementary knowledge: In pgAdmin 4, setting the "application_name" parameter to "Default" is to specify
the application name for the PostgreSQL client connection. This is a way to identify connections in the database and can help us more easily identify different sources of database connections.

When we set "application_name" to "Default" it means that we want to identify the current database connection as the default application, usually this refers to the client using pgAdmin 4 to connect to the PostgreSQL database. This is useful for database logs and performance analysis as it gives us a clear idea of ​​which application or tool created a specific database connection.

By setting the "application_name" parameter, we can create different names for different client applications or connections, making it easier for database administrators to identify and track the origin of individual connections. This is useful for debugging and monitoring the database, especially in environments with multiple client applications.

Setting "application_name" is mainly to better manage and track connections to distinguish connections from different applications or clients. This is particularly useful in environments with multiple applications connecting to the database simultaneously. If we are only using an application or tool to connect to the database and do not require a specific identity, then it is also possible not to set "application_name" as the default value is usually sufficient.

Unless there are special needs, the [Advanced] page generally does not need to be set.

In the [SQL] page, you can view the SQL statements executed when creating the database, which includes all previous settings.

In fact, the process of creating a database is the process of executing all the database building SQL statements in the [SQL] page one by one in the PostgreSQL server.
Insert image description here
If there is no problem after checking the relevant creation options, click "Save" to start executing the SQL statement and create the database.

In pgAdmin 4, viewing the SQL statement to be executed on the [SQL] page is a very useful function.

This allows us to view and review the SQL statements that will be executed when creating the database to ensure that all parameters and settings meet our needs.

Tips:

Generally speaking, we can follow the following steps to view the SQL statements when creating the database in pgAdmin 4:

  1. Open pgAdmin 4 and connect to our PostgreSQL server.

  2. In the Objects resource tree on the left, expand Databases, then right-click Create and select Database to open the database creation dialog box.

  3. In the dialog box, fill in the various parameters and settings of the database, such as name, owner, character set, connection restrictions, etc.

  4. At the bottom of the dialog box, you'll see a tab or tab bar, often called "SQL" or "SQL Pane," where you can view the SQL statements that are about to be executed.

  5. Preview the SQL statements and make sure they match your expectations. Here we can inspect and customize SQL statements.

  6. If everything meets the requirements, click "OK" or "Save" to execute the SQL statement and create the database.

This feature allows us to understand and control every detail in the database creation process to ensure that the database is created according to our requirements, which is very useful for database management and maintenance.

After clicking "Save", you can see that the database has been created.
Insert image description here
2) Use SQL commands to create a database

In addition to using visual tools (such as pgAdmin 4) to create databases, we can also use SQL commands to create databases.

In PostgreSQL, we can use the CREATE DATABASE statement to do this. Here's a basic example:

CREATE DATABASE mydatabase;

This will create a new database named "mydatabase".

We can replace "mydatabase" with the name of the database we wish to create. Additional options are available to specify the character set, owner, and other properties of the database.

For example, if we wanted to create a database with a specified character set and owner, we could do this:

CREATE DATABASE pgtest
  WITH OWNER = postgres
       ENCODING = 'UTF8';

This will create a database named "pgtest", set its owner to "postgres", and set the character set to UTF-8.

Click [Tools] – [Query Tool]

Insert image description here
You can see that a query window appears on the right, which is where SQL instructions are entered.

Copy and paste the CREATE DATABASE statement above into the SQL window.

Insert image description here
Click the "Execute" button, or press F5 to execute the SQL statement just entered.
Insert image description here
Look at the “Message” window at the bottom, which shows that the database has been created successfully.

"CREATE DATABASE took 49 milliseconds to return the query successfully."

Insert image description here
Right-click to refresh the database list
Insert image description here
and you can see the newly created "pgtest" database.

Insert image description here
You can view and edit database-related properties in the right window.

Creating a database using SQL commands provides more control and customization options for automated and batch operations.

However, we need to ensure that we have the appropriate database creation permissions to execute these commands.

When using SQL commands to create a database, we can also use the same method to view the SQL statements that create the database for recording or debugging.

3. Delete the database

If a database is no longer needed or needs to be deleted in actual work, we can use pgAdmin 4 to visually delete the database, or use SQL commands to delete the database.

3.1,Visually delete the database in pgAdmin 4:

  1. Open pgAdmin 4 and connect to your PostgreSQL server.
  2. In the Objects resource tree on the left, expand Databases.
  3. Locate the database you want to delete, right-click it and select Delete/Drop.
  4. In the confirmation dialog box, confirm that you want to delete the database and click OK.
    Insert image description here
    This will delete the database using a visual interface.

Tips:
When dropping a database in pgAdmin 4, there are two options to choose from: Drop (Drop) and Drop (Cascade). There are important differences between the two:

  1. Drop: This is a regular database deletion operation. When we choose to delete a database, pgAdmin 4 will try to delete the database, but it can only be successfully deleted if no other database objects (such as tables, views, functions, etc.) depend on the database being deleted. If there are dependencies related to the database, the delete operation will fail and the database will remain unchanged.

  2. Drop (Cascade): This is a forced delete operation. When we choose to force delete a database, pgAdmin 4 will try to delete the database, and before deleting, it will recursively delete all dependencies related to the database, such as tables, views, functions, etc. This means that if there are other database objects that depend on the database being deleted, they will also be deleted so that the entire database is deleted.

The difference is that "Drop" attempts to drop the database but retains associated dependencies, while "Drop (Cascade)" attempts to drop the database and removes all associated dependencies. Which method you choose depends on your needs and intentions:

  • If we want to delete only the database but keep other objects related to it, we can choose "Drop".
  • If we wish to delete the database and all objects related to it, we can select "Drop (Cascade)".

Please note that you need to be careful when performing a force delete operation as it will permanently delete all related objects, not just the database itself. Make sure you have backed up important data and objects before performing force deletion to avoid unnecessary data loss.

3.2,Use SQL command to delete the database:

In SQL, use the DROP DATABASE statement to drop the database. Here's an example:

DROP DATABASE pgtest;

This will delete the database named "pgtest". To ensure careful operation, it is recommended to back up the database to be deleted before deleting it, because deleting the database will permanently lose all data in the database.

No matter which method you use, you need the appropriate permissions to perform the deletion. In order to protect data security or prevent accidental data loss, in practice, only super users or users with database deletion permissions, such as database administrators (DBA), can perform this operation.

In summary, you can delete a database visually using pgAdmin 4, or you can delete a database using SQL commands, depending on your needs and preferences.

Guess you like

Origin blog.csdn.net/GYN_enyaer/article/details/133855293