0% found this document useful (0 votes)
22 views7 pages

Databasecommandinstal

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views7 pages

Databasecommandinstal

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Step 1 – Update Server and Install Pre-Requisites

Once you are logged in to your server, run the following command to update your base system with
the latest available packages.
apt-get update -y

Install the following prerequisites:


apt install gnupg gnupg2 gnupg1 -y

Step 2 – Install PostgreSQL 14


The latest version of PostgreSQL is not included in the Ubuntu default repository, so you will need
to add the PostgreSQL official repository to the APT.
You can add it with the following command:
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" >
/etc/apt/sources.list.d/pgdg.list'

Next, download and add the PostgreSQL GPG key using the following command:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

Next, update the repository cache and install PostgreSQL 14 using the following command:
apt-get update -y
apt-get install postgresql-14 -y

Please be aware that the configured PostgreSQL repository mentioned above is capable of
downloading the most recent version of PostgreSQL. As of this update, PostgreSQL is operating on
version 15, with the release of version 16 imminent. To ensure the installation of the latest version
at any time, modify the command above as follows:
apt-get -y install postgresql

After installing PostgreSQL, check the status of the PostgreSQL with the following command:
systemctl status postgresql

You will get the following output:


● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Fri 2021-11-26 04:57:30 UTC; 43s ago
Main PID: 17268 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 2353)
Memory: 0B
CGroup: /system.slice/postgresql.service

Nov 26 04:57:30 ubuntu2004 systemd[1]: Starting PostgreSQL RDBMS...


Nov 26 04:57:30 ubuntu2004 systemd[1]: Finished PostgreSQL RDBMS.

You can also verify the PostgreSQL version using the command below:
sudo -u postgres psql -c "SELECT version();"

You will get the following output:


version
------------------------------------------------------------------------------------------------
----------------------------------
PostgreSQL 14.1 (Ubuntu 14.1-2.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu
9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit

Step 3 – Create a Database and User in PostgreSQL


First, you can connect to PostgreSQL with the following command:
su - postgres
psql

You will get the following shell:


psql (14.1 (Ubuntu 14.1-2.pgdg20.04+1))
Type "help" for help.

postgres=#

Next, create a superuser named root with the following command:


CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'securepassword';

Run the following command to verify the superuser:


\du

You will get the following output:


List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
root | Superuser, Create role, Create DB | {}

To create a database named dbname, run:


create database dbname;

To create a new user named dbuser, run:


create user dbuser with encrypted password 'dbpassword';

To grant all privileges to the dbname to dbuser, run:


grant all privileges on database dbname to dbuser;

To list all databases, run:


\l

You will get the following output:


List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
dbname | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | dbuser=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)

To exit from the PostgreSQL shell, run:


\q
exit

Step 4 – Configure PostgreSQL for Remote Access


By default, PostgreSQL is configured for local access only. If your application and database servers
are hosted on a different server, then you must configure PostgreSQL for remote access. You can
configure it by editing the file pg_hba.conf:
nano /etc/postgresql/14/main/pg_hba.conf

Find the following line:


local all all peer

And replace it with the following line:


local all all trust

Next, add the following line:


host all all 0.0.0.0/0 md5

Save and close the file when you have finished.


Next, you will also need to edit the PostgreSQL main configuration file and change the listening
port:
nano /etc/postgresql/14/main/postgresql.conf

Change the following line:


listen_addresses='*'

Save and close the file, then restart the PostgreSQL service to apply the changes:
systemctl restart postgresql

You can now check the PostgreSQL listening port using the following command:
ss -antpl | grep 5432
You will get the following output:
LISTEN 0 244 0.0.0.0:5432 0.0.0.0:*
users:(("postgres",pid=19030,fd=5))
LISTEN 0 244 [::]:5432 [::]:*
users:(("postgres",pid=19030,fd=6))

Now, go to the remote system and connect to the PostgreSQL instance using the following
command:
psql 'postgres://root:securepassword@104.245.34.223:5432/postgres?sslmode=disable'

If everything is fine, you will get the following shell:


psql (14.1 (Ubuntu 14.1-2.pgdg20.04+1))
Type "help" for help.

postgres=#

3. Install pgAdmin 4
Install all required dependencies for pgAdmin 4 installation.
$ sudo apt-get install curl gnupg2 -y

Import the repository PGP signing key for pgAdmin.


$ sudo curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add

Add pgAdmin 4 APT repository.


$ sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs)
pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt-get update --allow-insecure-
repositories'

Install the web-based version of pgAdmin 4 tool for your platform.


$ sudo apt install pgadmin4-web -y

Configure the web mode. You'll be required to enter an email address and a password for later login.
$ sudo /usr/pgadmin4/bin/setup-web.sh

For UFW firewall configured, allow http or https traffic depending on what you are using.
$ sudo ufw allow http
$ sudo ufw allow https

After installation, open your browser and go to http://ServerIP/pgadmin4 to access the


pgAdmin 4 user interface. For example:
http://192.0.2.48/pgadmin4
Login with the email address and password that you set during pgAdmin 4 web mode
configuration.

4. Connect to Database Server on pgAdmin 4


On the web interface of your preinstalled pgAdmin 4, locate the dashboard and click, Add New
Server, you'll get a dialogue box where you'll enter your preferred name for that server.

Enter the preferred name of the server in the Name input section.

On the pop up dialogue box, switch to the Connection tab and enter the required fields with the
appropriate data.
1. Enter your IP address under the Host name/address.
2. Leave Port value as 5432.
3. Enter testdb under Maintenance database.
4. Enter pgadmin under Username.
5. Enter secure_password as password. Modify the secure_password value with the actual
value used in Step 1.
6. Click Save to login to the server.

After the connection is successful, go to the upper left corner of the dashboard, and you will see
your server. browse through by expanding the arrows to access your database testdb.
Conclusion
You have now successfully browsed to the database. You'll be able to see the database server
metrics on the main dashboard on graphs. You can then begin to fully manage the server and add
more databases.

You might also like