.NET Command-Line Interface (CLI)
Once you have installed .NET, the CLI can be used to create and configure projects for use with VS Code. To launch the .NET CLI, run the following at the command prompt:
dotnet
If .NET is installed correctly, you will see the following message on your screen:
Usage: dotnet [options] Usage: dotnet [path-to-application]
Once you have the CLI installed to configure projects with VS Code, you need to know about the powerful open source object-relational database system that uses and extends the SQL language that is, PostgreSQL.
Note
You will first go through the instructions to install PostgreSQL for Windows followed by macOS, and then by Linux.
PostgreSQL Installation for Windows
PostgreSQL has been used in Chapter 6, Entity Framework with SQL Server. Before you proceed with that chapter, you must install PostgreSQL on your system using the following steps:
- Go to https://www.enterprisedb.com/downloads/postgres-postgresql-downloads and download the latest version installer for Windows:
Note
The screen shown in Figure 0.8 may change depending upon the latest release from the vendor.
- Open the downloaded interactive installer and click the
Next
button. TheSetup PostgreSQL
screen gets displayed:
- Click on the
Next
button to move to the next screen which asks for the installation directory details:
- Leave the default
Installation Directory
unchanged and click theNext
 button. - Select the following from the list in Figure 0.11:
PostgreSQL Server
refers to the database.pgAdmin 4
is the database management tool.Stack Builder
is the PostgreSQL environment builder (optional).Command Line Tools
work with the database using a command line.
- Then click the
Next
button. - In the next screen, the Data Directory screen asks you to enter the directory for storing your data. So, enter the data directory name:
- Once you have entered the data directory, click on the
Next
button to continue. The next screen asks you to enter the password. - Enter the new
Password.
- Retype the password beside
Retype password
for the database superuser:
- Then click the
Next
button to continue. - The next screen displays the Port as
5432
. Use the default port—that is,5432
:
- Click the
Next
button. - The Advanced Options screen asks you to type the locale for the Database cluster. Leave it as
[Default locale]
:
- Then click the
Next
button. - When the Preinstallation Summary screen gets displayed, click the
Next
button to go ahead:
- Continue selecting the
Next
button (leaving the default settings unchanged) until the installation process begins. - Wait for it to complete. On completion, the Completing the PostgreSQL Setup Wizard screen gets displayed.
- Uncheck the
Launch Stack Builder at exit
option:
The Stack Builder is used to download and install additional tools. The default installation contains all tools needed for the exercises and activities.
- Finally, click the
Finish
button. - Now open
pgAdmin4
from Windows. - Enter a master
Password
for connecting to any database inside PostgreSQL in the Set Master Password window:
Note
It is better to type a password that you can easily memorize as it will be used to manage all your other credentials.
- Next click the
OK
button. - On the left side of the pgadmin window, expand the
Server
by clicking the arrow beside it. - You will be asked to enter your PostgreSQL server password. Type the same password that you entered in Step 22.
- Do not click
Save password
for security reasons:
PostgreSQL server password is the password you will use when connecting to the PostgreSQL server and using the postgres
user.
- Finally click the
OK
button. You will see the pgAdmin dashboard:
In order to explore the pgAdmin dashboard, move to the Exploring pgAdmin Dashboard section.
PostgreSQL Installation for macOS
Install PostgreSQL on your macOS using the following steps:
- Visit the official site of the Postgres app to download and install PostgreSQL on your mac platform: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads.
- Download the latest PostgreSQL for macOS:
Note
The following screenshots were taken for version 14.4 on macOS Monterey (version 12.2).
- Once you have downloaded the installer file for macOS, double-click the
installer file
to launch the PostgreSQL Setup Wizard:
- Select the location where you want PostgreSQL installed:
- Click on the
Next
button. - In the next screen, select the following components for installation:
- PostgreSQL Server
pgAdmin 4
- Command Line Tools
- Uncheck the
Stack Builder
component:
- Once you have selected the options, click on the
Next
button. - Specify the data directory in which PostgreSQL will store the data:
- Click on the
Next
button. - Now set a
Password
for the Postgres database superuser:
Make sure to note down the password safely for logging in to the PostgreSQLÂ database.
- Click on the
Next
button.
Set the port number where you want to run the PostgreSQL server. Here the default Port number is set as 5432
:
- Click on the
Next
button. - Select the locale to be used by PostgreSQL. Here,
[Default locale]
is the locale selected for macOS:
- Click on the
Next
button. - In the next screen, check the installation details:
Finally, click on the Next
button to start the installation process of the PostgreSQL database server on your system:
- Wait for a few moments for the installation process to complete:
- When prompted, click the
Next
button. The next screen displays the message that the PostgreSQL installation is complete on your system:
- Click the
Finish
button once the installation gets complete. - Now load the database in the PostgreSQL server.
- Double-click on the
pgAdmin 4
icon to launch it from your Launchpad. - Enter the password for the PostgreSQL user that you had set during the installation process.
- Then click the
OK
button. You will now see the pgAdmin dashboard.
This completes the installation of PostgreSQL for the macOS. The next section will familiarize you with the PostgreSQL interface.
Exploring pgAdmin Dashboard
Once you have installed PostgreSQL in Windows and macOS, follow these steps to grasp the interface better:
- Open
pgAdmin4
from Windows/ macOS (in case pgAdmin is not open on your system). - Click on the
Servers
option on the left:
- Right-click on
PostgreSQL 14
. - Then click on the
Create
option. - Choose the
Database…
option to create a new database:
This will open a Create – Database window.
- Enter the database name, as
TestDatabase
. - Select the Owner of the database or leave it as default. For now, just use the
Owner
aspostgres
:
- Then click on the
Save
button. This will create a database. - Right-click on
Databases
and choose theRefresh
button:
A database with the name TestDatabase
is now displayed within the dashboard:
Now your database is ready to be used for Windows and Mac environments.
PostgreSQL Installation on Ubuntu
In this example, you are using Ubuntu 20.04 for installation. Perform the following steps to do so:
- In order to install PostgreSQL, open your Ubuntu terminal first.
- Make sure to update your repository using the following command:
$ sudo apt update
- Install the PostgreSQL software along with additional packages using the following command (recommended):
$ sudo apt install postgresql postgresql-contrib
Note
To install only PostgreSQL (not recommended without additional packages), use the command
$ sudo apt install postgresql
and then pressEnter
.
This installation process creates a user account called postgres
that has the default Postgres
role.
Accessing the postgres User Account with the postgres Role
There are two ways to start the PostgreSQL CLI using the postgres
user account:
Option 1 is as follows:
- To log in as a postgres user, use the following command:
$ sudo -i -u postgres
- Access the CLI by using the following command:
$ psql
Note
Sometimes, while executing the preceding command, a
psql
error may be displayed ascould not connect to server: No such file or directory
. This is because of a port issue on your system. Due to this port blockage, the PostgreSQL application may not work. You can try the command again after some time. - To quit the CLI, use the following command:
$ \q
Option 2 is as follows:
- To log in as a postgres user, use the following command:
$ sudo -u postgres psql
- To quit the CLI, use the following command:
$ \q
Verifying the postgres User Account as a postgres User Role
- To verify the user account, log in and use the
conninfo
command:$ sudo -u postgres psql $ \conninfo $ \q
Using this command, you can ensure that you are connected to the postgres
database as the postgres
user via port 5432
. If you don't want to use the default user, postgres
, you can create a new user for you.
Accessing a New User and Database
- Create a new user by using the following command and pressing
Enter
:$ sudo -u postgres createuser –interactive
The preceding command will ask the user to add the name of the role and its type.
- Enter the name of the role, for example,
testUser
. - Next, enter
y
when prompted to set a new role to be a superuser:Prompt: Enter the name of the role to add: testUser Shall the new role be a superuser? (y/n) y
This will create a new user with the name testUser
.
- Create a new database with the name
testdb
using the following command:$ sudo -u postgres createdb testdb
- Log in to the newly created user account using the following command:
$ sudo -u testUser psql -d testdb
- Use the following command to check the connection details:
$ \conninfo
- To quit the CLI, use the following command:
$ \q
Using this command, you can ensure that you are connected to the testdb
database as the testUser
user via port 5432
.
With these steps, you have completed the PostgreSQL installation for Ubuntu.