0% found this document useful (0 votes)
125 views

Postgres Postgresql Downloads

PostgreSQL is an open-source relational database management system that supports both SQL and non-SQL querying. The document discusses how to install PostgreSQL on Windows, verify the installation, and connect to the PostgreSQL database server using psql and pgAdmin tools. Key steps include downloading the PostgreSQL installer, selecting installation options, setting a password for the postgres user, verifying the installation by launching psql and checking the PostgreSQL version, and connecting to the server via psql or pgAdmin's GUI interface by providing the host, port, and postgres password.

Uploaded by

Achmad Ardi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Postgres Postgresql Downloads

PostgreSQL is an open-source relational database management system that supports both SQL and non-SQL querying. The document discusses how to install PostgreSQL on Windows, verify the installation, and connect to the PostgreSQL database server using psql and pgAdmin tools. Key steps include downloading the PostgreSQL installer, selecting installation options, setting a password for the postgres user, verifying the installation by launching psql and checking the PostgreSQL version, and connecting to the server via psql or pgAdmin's GUI interface by providing the host, port, and postgres password.

Uploaded by

Achmad Ardi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PostgreSQL

Introduction How to Install, verify and connect to database server


on PostgreSQL
- PostgreSQL (pronounced as post – gress – Q – L)
is an open – source relational database - Steps to install PostgreSQL
management system (DBMS). 1. Download PostgreSQL installer for windows
- PostgreSQL supports both SQL (relational) dan
JSON (non -relational) querying https://www.enterprisedb.com/downloads/
- Relational vs. non – relational databases ? postgres-postgresql-downloads
a. Relational
Is structured, i.e the data is organized in tables 2. Install PostgreSQL
b. Non – relational a. Step 1 : double click on the installer file,
Is document – oriented, which means, all an installation wizard will appear and
information gets stored in more of a laundry guide you through multiple steps where
list order you can choose different options that you
- In simple words, PostgreSQL is a system that would like to have in PostgreSQL
helps you in managing your huge databases by b. Step 2 : click the next button
hiding the underlying complicated details. c. Step 3 : specify the installation folder,
- PostgreSQL features : choose your own or keep the default
1. Compatible with various platforms using all folder suggested by the PostgreSQL
major languages and middleware installer and click the next button
2. It offers a most advanced locking mechanism d. Step 4 : select software components to
3. Support for multi – version concurrency install
control 1) PostgreSQL Server – to install the
4. Mature server – side programming PostgreSQL database server
functionality 2) pgAdmin 4 – to install the
- MySQL vs. PostgreSQL ? PostgreSQL database GUI
a. The MySQL project has made its source code management tool
available under the terms of the GNU license, 3) command line tools – to install
and other proprietary agreements, whereas command line tools such as psql,
PostgreSQL is released under PostgreSQL pg_restore, etc
license e. Step 5 : select the database directory to
b. MySQL is now owned by Oracle corporation store the data or accept the default folder.
and offers several paid editions, whereas And click the next button to go to the next
PostgreSQL is free and open – source step
software. That means you will never need to f. Step 6 : enter the password for the
pay anything for this service database superuser (postgres) ex : 123456
- Advantages of PostgreSQL g. Step 7 : enter a port number on which the
1. PostgreSQL source code is freely available PostgreSQL database server will listen.
under an open – source license The default port of PostgreSQL is 5432.
2. PostgreSQL supports geographic objects so Make sure that no other applications are
you can use it for location – based services using this port
and geographic information systems h. Step 8 : choose the default locale used by
3. PostgreSQL allows you the freedom to use, the PostgreSQL database. If you leave it
modify, and implement it as per you needs as the default locale, PostgreSQL will use
- Disadvantages of PostgreSQL the operating system locale
1. Changes made for speed improvement i. Step 9 : click the finish button to complete
requires more work than MySQL as the PostgreSQL installation
PostgreSQL focuses on compatibility - Verify the installation
2. Many open source apps support MySQL, but 1. They are several ways to verify the
may not support PostgreSQL PostgreSQL installation
3. On performance metrics, it is slower than 2. The quick way to verify the installation is
MySQL through the psql program
3. First, click the psql application to launch it.
the psql command – line program will display

4. Second, enter all the necessary information


such as the server, database, port, username,
and password. To accept the default, you can
press enter

Note. That you should provide the password


that you entered during installing the
PostgreSQL, type specifically username
(postgres) and then type password (just try
typing your password out, and hitting enter)

5. Third, write the command SELECT version();


you will see the following output:

Congratulations! You’ve successfully


installed the PostgreSQL database server on
your local system
- Connecting to database server
When you installed the PostgreSQL database
server, the PostgreSQL installer also installed
some useful tools for working with the
PostgreSQL database server.
In this tutorial, you will learn how to connect
to the PostgreSQL database server via the
following tools:
1. psql – a terminal-based front-end to
PostgreSQL database server
a. psql is an interactive terminal program
provided by PostgreSQL
b. It allows you to interact with the
PostgreSQL database server such as
executing SQL statements and managing
database objects
c. Steps
1) Launch the psql program and connect
to the PostgreSQL Database server
using the postgres user (SQL Shell
(psql))
2) Enter all the information such as
Server, Database, Port, Username, and
Password. If you press enter, the
program will use the default value
specified in the square bracket [ ] and
move the cursor to the new line.

For example, localhost is the default


database server. In this step for
entering the password for user
postgres, you need to enter the
password of the user postgres that you
choose during the PostgreSQL
installation
3) Interact with the PostgreSQL database
server by writing an SQL statement

SELECT version( );

Note* please do not forget to end the


statement with a semicolon (;)

After pressing enter, psql will return


the current PostgreSQL version on
your system
2. pgAdmin – a web-based front-end to
PostgreSQL database server
a. The pgAdmin application allows you to
interact with the PostgreSQL database
server via an intuitive user interface (GUI
application)
b. Steps
1) Launch the pgAdmin application – the
pgAdmin application will launch on
the web browser
2) Right – click the servers node and
select Create > Server…menu to
create a server
3) Enter the server name, for example :
PostgreSQL, and click the connection
tab
4) Enter the host and password for the
postgres user and click the save button

Host : localhost
Port : 5432
Maintenance database : postgres
Password : 123456

5) Clik on the servers node to expand the


server – by default, PostgreSQL has a
database named postgres as shown
below :

Image..

6) Open the query tool by choosing the


menu item tool > query tool or click
the lightning icon
7) Enter the query in the Query editor,
click the execute button, you will see
the result of the query displaying in
the data output tab :

SELECT version( );

This is how you can connect to the


PostgreSQL database server by using different
client tools including psql and pgAdmin GUI
aplication
All about database

- Encounter the below errors while we are using the


create database command :
1. No such file in the server is executing locally
and accepting connections on the Unix
domain socket
-> if we use the create database command and
the server is not started then we get the error
2. Permission is not granted to create a database
-> this error will occur when we need to give
authorization to the related users for using the
create command
3. PostgreSQL account is created but does not
have access to create a database
4. Create database command is not found
-> If PostgreSQL is not installed correctly, we
may encounter this type of error, and we need
to execute the create database command from
our PostgreSQL installation path
- Creating PostgreSQL database
a. In PostgreSQL, we can create a database in
two ways :
1. PostgreSQL create database using
pgAdmin
Step
1) Firstly, we will open the pgAdmin in
out local system, and then in the
Object tree, we will right – click on
the databases and select Create then
select the database
2) After that, the create database window
will open where we need to provide
some necessary details (Database
name, Comment) for creating a
database and then click on the Save

3) The database is created and displays in


the Object tree

4) And the right – hand side window will


give us the SQL which is used to
create the Database
Explanation :
1) db_name : this parameter is used
to specify the new database name,
which we want to create
2) note* make sure that the database
name must be unique because if
we try to create a new database
with the same name as an existing
database, it will show an error
3) role_name : it is used to describe
the role name for the user who will
have the new database, and by
default, it is postgres
4) template : while creating the new
database, we will require a
database template name
5) encoding : it is used to describe the
character set encoding for the new
database, and by default, it is
UTF8
6) collate : it is used to define the sort
order of strings that mark the result
of the ORDER BY clause if we are
using a SELECT statement
7) ctype : this parameter is used to
display the character classification
for the new database
8) tablespace_name : it is used to
define the tablespace name for the
new database, and by default, it is
the template database’s tablespace
9) max_concurrent_connection : this
parameter is used to define the
maximum parallel connections of a
new database, and by default, it is
-1 (unlimited)

2. PSQL create database command line


(SQL Shell)
1) We will open the SQL shell in our
local system. For this, we will go to
the home button and search for pSQL
and open it
2) Once the SQL shell is opened, we will
press the enter key for the 4 – 5 times
and then provide the password for the
user (which we created earlier) to
connect the database
3) Enter the bellow command to create a
database

CREATE DATABASE db_name;

4) To get a list of all databases created


earlier, we will enter the below
command

\l;

5) To connect to a database, we will enter


the below command

\c db_name;

- Selecting PostgreSQL database


a. With the help of pgAdmin, we can select the
database. Follow the below steps for
accessing the database in PostgreSQL :
1. Step 1 : First, we will open the pgAdmin
UI and SQL Query window
2. Step 2 : then, we will select the database
by clicking on it
3. Step 3 : after that, we will click on the
tools drop – down menu, and click on
Query tool
4. Step 4 : now, a new window appears with
a connection made to the database we
selected, and here we can run SQL queries

- Droping a database
a. We are going to learn how to drop or delete
the database which we don’t need anymore in
PostgreSQL
b. The drop / delete command is used to
eternally delete all the file entries and data
directories from the PostgreSQL platform
c. Therefore, we have to use this command very
carefully.
d. In PostgreSQL, we can drop the database in
two ways :
1. Drop database PgAdmin
2. Drop database using SQL shell
(Command Line)
e. Drop database using PgAdmin
1. Step 1 : firstly, we will open the pgAdmin
in our local system
2. Step 2 : select the database by a left –
click on it
3. Step 3 : then right – click on the database
then clik on delete / drop option from the
given drop – down list to delete the
database
4. Step 4 : database will be deleted
immediately from the record
f. Drop database using SQL shell (Command
Line)
We’re going to drop the database in the SQL
shell. The syntax for dropping the database
is :

DROP DATABASE [IF EXISTS] name;

IF EXISTS : it is an optional parameter where


the warning is displayed in the place of an
error if the database does not exists

name : here, we will reference the database


name that we want to drop

1. Step 1 : open the SQL shell and type the


below command to see the existing
database

\l

2. Step 2 : for dropping the database, we will


enter the below command

Drop database db_name;

Note* : while using this above command,


we may encounter the below error :

ERROR : database “db_name” is being


accessed by other users.
Detail : there is 1 other session using the
database

3. Step 3 : to delete the database called


“db_name”, first we have to revoke the
connection with the help of the below
command and then press the enter key :

REVOKE CONNECT ON DATABASE


db_name from public;

Note* : once the connection is Revoked,


we will enter the following query:

SELECT
pg_terminate_backend(pg_stat_activity.pi
d)
FROM pg_stat_activity
WHERE pg_stat_activity.datname =
“db_name”;

4. Step 4 : then enter the drop database query


and use the \l command to verify whether
the database is deleted or not
5. Step 5 : if we try to drop the same
database again, we will get the following
error.

ERROR: database “db_name” does not


exist

6. Step 6 : then we are going to drop the


database with IF exist condition, and we
get the below warning :

NOTICE: database “db_name” does not


exist, skipping DROP DATABASE

g. dropdb
a) In PostgreSQL, the dropdb is a command
– line executable command which covers
the SQL drop database command
b) This command can only be run by those
end – users who are the owner of the
database or a database superuser
c) We can remove the database remotely
with the help of the dropdb statement
d) The syntax for dropdb command is,

Dropdb [option…] dbname

e) Example

dropdb -h localhost -p 5432 -U postgres


dbname

password for user postgres : ****

Note* : here we will use the Postgres


username to drop the database. The above
command drops the dbname database

Options Description
-e Here e means Echo, which is used to create
and send to the server
-i It is used to show the verification prompt
before operating any fatal job
--help It allows us the help with dropdb
command-line statements
-h host It define the names of the host of the
system, where the server is directly
executing
-p port This option defines the Unix domain socket
file extension, where the server is creating
the connections
-v We can use -v option, to print the dropdb
version
-U username It is used to display the user name
-w If we don’t need a password screen, we can
use the option
Maintenance To connect the database for dropping the
dp-=dbname target database, we will use this option to
describe the database name
--if exists This option will display an error rather than
a warning if the database does not exist
-w This option is used to prompt for a
password before dropping the database

You might also like