Today is my day 1 of learning Postgres let's learn together, are you ready to dive into the world of powerful relational databases? If so, PostgreSQL, commonly referred to as PSQL, is an excellent choice to begin your journey. Whether you're a developer, data analyst, or database enthusiast, PostgreSQL offers a robust and feature-rich environment for managing and querying your data. In this beginner's guide, we'll take you through the fundamental concepts of PostgreSQL and help you get started on your path to becoming a PSQL pro.
Let's start with installing PSQL
I started by visiting the official PostgreSQL website https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, and download the installer suitable for my operating system. The website offered options for various platforms, including Windows, macOS, and different Linux distributions.
Run the downloaded installer. Follow the installation wizard, and make sure to remember the password you set for the Postgres user.
During installation, you'll be prompted to select the installation directory and port number. The default values are generally fine, but you can customize them if needed.
Open the Start menu and search for "pgAdmin." This is a graphical management tool for PostgreSQL.
Once you open the pgAdmin you will get a prompt to enter the password which you created during installation.
Postgres is a default database provided out of the box by the software
Let's create your first Database using the PSQL shell.
Yes, you read it right we are going to use PSQL shell or PSQL tool to write SQL queries, it is the best way to learn and practice SQL.
Select the 'Postgres' DB and click the PSQL tool, this will open the PSQL shell in the pgAdmin itself.
You will get the shell like this.
Let's create a new Database and connect to it.
Creating a new database and connecting to it can be done by the UI itself, but it won't be as much as effective for learning and practice and using commands in the shell will make us better developers.
CREATE DATABASE testdb;
write the create database query with a semicolon and hit enter. you will get the confirmation message below.
we can see the available databases by the \l
command. our database testdb was created successfully.
Here comes the connecting to a database part. we can connect to a database in two different ways
1) Currently we are in the default 'Postgres' database from here we can connect to our testdb database by simply typing the following command.
\c testdb
2) before talking about the second way let's see about quitting from the PSQL shell, we can simply use \q
to come outside of the PSQL shell.
Now we are outside of the PSQL shell let's see how to connect to our testdb form here.
There are four parameters required to connect to a DB.
-h (host) --> localhost
-p (port) --> 5432 (default port)
-U (username) (NOTE: use Capital U --> in our case 'postgres' is the user name.)
database name
psql -h localhost -p 5432 -U postgres testdb
Now we have established a successful connection to our testdb.
That's it for this blog stay tuned for the upcoming chapters where we'll explore each topic in detail, accompanied by practical examples. Get ready to unlock the full potential of PostgreSQL and elevate your data management skills to new heights.