Create Database in PostgreSQL using psql and pgAdmin
PostgreSQL database can be created using psql (SQL Shell) and pgAdmin.
Create Database using psql
Use the CREATE DATABASE command in psql to create a new database in PostgreSQL.
CREATE DATABASE <databse_name>
Let's create the 'HR' database in psql.
Now, we can check whether the database is created or not by using \l
command which will display the list of databases on the server.
The postgres=#
in psql indicates the current database name. At this point it is pointing to the default postgres
database. All the tables and other database objects you will be created in the postgres
database.
We can switch the current database using the \connect database-name
or \c database-name
command. Let's switch to our new 'HR' database.
Create Database using pgAdmin
We can also create a new database in PostgreSQL using GUI based tool pgAdmin.
Open pgAdmin and right-click on the server name (here it is default server name PostgreSQL 14) -> Create -> Database..., as shown below.
This will open Create – Database dialog, as shown below. Here you can provide a Database name, select owner (postgres will be the owner by default). You can also provide other settings in Defination, Security, Parameters, Advanced, and SQL tabs. Click on Save to create this new database.
pgAdmin will list a newly created database under the Databases node in the left pane, as shown below.
Click on the database name to expand it, as shown below.
Thus, you can create and configure a new database in a nice UI-based pgAdmin tool.