sql Archives - The Linux GURUS https://thelinuxgurus.com/tag/sql/ Learn Linux & DevOPS from THE LINUX GURUS Tue, 28 Apr 2020 14:13:08 +0000 en-US hourly 1 https://i0.wp.com/thelinuxgurus.com/wp-content/uploads/2020/01/cropped-thelinuxgurus_transparent_name.png?fit=32%2C32&ssl=1 sql Archives - The Linux GURUS https://thelinuxgurus.com/tag/sql/ 32 32 148921671 Beginner’s guide to Backup Postgres Database https://thelinuxgurus.com/beginners-guide-to-backup-postgres-database/ https://thelinuxgurus.com/beginners-guide-to-backup-postgres-database/#respond Tue, 28 Apr 2020 14:14:22 +0000 https://thelinuxgurus.com/?p=863 Backups are important & they are our only contingency for when we delete things that we should not or there are malfunctions hardware or otherwise....

The post Beginner’s guide to Backup Postgres Database appeared first on The Linux GURUS.

]]>
Backups are important & they are our only contingency for when we delete things that we should not or there are malfunctions hardware or otherwise. Backups at regular intervals are essential for any organization to maintain a proper working environment. Same goes for database, database backups are extremely essential as it holds transactional, user data among other things.

In this tutorial, we will learn about PostgreSQL backup. Before we start this, you can also refer to our tutorials on installing Postgresql on CentOS/RHEL, also for installation on Ubuntu & installing Postgresql from the source.

Recommended Read: Important postgresql commands for beginners


Backup Postgres Database


Postgresql provides backup utilities when we install postgresql on the server, they are pg_dump & pg_dumpall. Pg_dump is most useful for taking postgresql backup of a single database, while pg_dumpall provides the postgresql backup for all databases on the server. Let’s see some examples.

Backup Postgres database, single DB

Start by switching to the user postgres,

# su – postgres

Now to back up a database, use the following command,

$ pg_dump -U user_name -d thelinuxgurus > thelinuxgurus.bak

Here, ‘thelinuxgurus’ is the name of the database we need to backup. We can also use .sql or .tar as extension to backup file.


Taking backup of one or more tables in postgres

Command to take the backup of a single table from a database is,

$ pg_dump -U User_name -d database_name -t public.users_table > users_table.bak

To take the backup of more tables,

$ pg_dump -U User_name -d database_name -t public.users_table public.test_table public.test_table_2 > 3_tables.bak


Restoring a database

Now if we need to restore the backup, we need to drop the old database & create an empty database,

$ dropdb thelinuxgurus

$ createdb new_db

Not to restore the backup, use the following command,

$ psql new_db < thelinuxgurus.bak

Or we can also use pg_restore utility to restore a database, use the following command to restore the database using pg_restore utility,

$ pg_restore -d db_name /path/to/your/file/thelinuxgurus.bak -c -U db_user


Restore one or more tables from backup

Similarly, we can also restore the backup of one or more tables,

$ pg_restore -U user_name –data-only -d target-db-name -t table_name /path/to/your/file/users_table.bak


Taking only schema backup

To take the backup of schema only, use the following command,

$ pg_dump –schema=schema_name db_name > schema.bak


Restoring schema backup

To restore schema backup, use the following command,

$ psql -d db_name -U user_name < schema.bak


Taking all database backup

The second backup utility i.e. pg_dumpall is used for taking backup of all the databases on postgreSQL. To take backup use the following command,

$ pg_dumpall > all_databases.bak


Restore all databases backup

To restore all database backup, use the following command,

$ psql -f all_databases.bak postgres

We can also use all or any of the above-mentioned commands & create a cron job for it to automate our backups. Please refer to our tutorial on Scheduling Cron jobs on Linux system. We now end this tutorial on backup Postgres database. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post Beginner’s guide to Backup Postgres Database appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/beginners-guide-to-backup-postgres-database/feed/ 0 863
How to install MySQL on Ubuntu https://thelinuxgurus.com/how-to-install-mysql-on-ubuntu/ https://thelinuxgurus.com/how-to-install-mysql-on-ubuntu/#respond Tue, 31 Dec 2019 06:04:10 +0000 https://thelinuxgurus.com/?p=879 MySQL is a popular Open Source Relational Database System aka RDBMS. MySQL uses a relational database & structured query language (SQL) to manage all the...

The post How to install MySQL on Ubuntu appeared first on The Linux GURUS.

]]>
MySQL is a popular Open Source Relational Database System aka RDBMS. MySQL uses a relational database & structured query language (SQL) to manage all the data. It’s used for developing all sorts of web-based applications.

It’s one of the most widely used database servers in the world & also the main component of the LAMP stack. In this tutorial, we will learn how we can install MySQL on Ubuntu.

Recommended Read: Simple guide to install POSTGRESQL on Ubuntu

Also Read: How to setup SSH login without password on Linux systems


Install MySQL on Ubuntu

Installation of MySql is fairly simple as the mysql packages are available with the default Ubuntu repositories. In order to install Mysql on Ubuntu, we only have run the following command from the terminal,

$ sudo apt-get update && sudo apt-get upgrade -y

$ sudo apt-get install mysql-server

Once the installation starts, we will be asked to enter a root password for MySQL. Enter a password and remember it as it will be required after installation, for configuration of mysql as well,

install mysql on ubuntu

Once the mysql has been installed, we need not start the mysql service as its started by default but we can check to make sure that its up & running by executing the following command,

$ sudo systemctl status mysql

Commands to start, stop & restart mysql service are,

$ sudo systemctl start mysql

$ sudo systemctl stop mysql

$ sudo systemctl restart mysql


Configuring MySQL

Next step after installing mysql is to secure the mysql installation, run the following command from the terminal,

$ sudo secure_mysql_installation

We will now be required to secure the installation of mysql by answering the following questions. To answer the questions positively. Press ‘Y’ or we can press any other key from the keyboard to answer in the negative,

1- Do we need to have a password policy for users accessing the DB. This is done with VALIDATE PASSWORD PLUGIN. If yes, what level of password policy & what should be the strength of password,

2- Remove anonymous users

3- Disallow root login remotely

4- Remove test database & its accessing

5- & lastly, Reload privileges table now.

install mysql on ubuntu

After completing the installation, we can now use the database. To connect to the database, run the following command from the terminal,

$ sudo mysql

Here we need to enter the root password that we created earlier during the installation of mysql. So that’s it, we now end our tutorial on how to install MySQL on Ubuntu. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post How to install MySQL on Ubuntu appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/how-to-install-mysql-on-ubuntu/feed/ 0 879
Recommended guide to install POSTGRESQL from Source https://thelinuxgurus.com/recommended-guide-to-install-postgresql-from-source/ https://thelinuxgurus.com/recommended-guide-to-install-postgresql-from-source/#respond Sat, 28 Dec 2019 15:59:22 +0000 https://thelinuxgurus.com/?p=859 PostgreSQL (also called postgres) is a very famous, powerful, open-source object-relational database management system that has been around for almost 30 years. PostgreSQL requires very...

The post Recommended guide to install POSTGRESQL from Source appeared first on The Linux GURUS.

]]>
PostgreSQL (also called postgres) is a very famous, powerful, open-source object-relational database management system that has been around for almost 30 years. PostgreSQL requires very minimum maintained efforts because of its stability. Therefore, if you develop applications based on PostgreSQL, the total cost of ownership is low in comparison with other database management systems.

Recommended Read: Simple guide to install POSTGRESQL on Ubuntu

Also Read: How to setup SSH login without password on Linux systems

In this tutorial, we will learn to install PostgreSQL from source. So let’s start the tutorial,


Pre-requisites

We need to install some packages on servers before we can install PostgreSQL from source,

CentOS/RHEL

# yum groupinstall “Development Tools” -y

# yum install gcc zlib-devel readline-devel systemd-devel -y

Ubuntu

# sudo apt-get install build-essential -y

# sudo apt install gcc zlib1g-dev libreadline6-dev libsystemd-dev -y

Now we can start the process to install PostgreSQL from source.


Install Postgres from source

The first step would be to download the source package for Postgres. Here we will be installing PostgreSQL 11.6,

# wget -O – https://ftp.postgresql.org/pub/source/v11.6/postgresql-11.6.tar.bz2 | tar jxf –

# cd postgresql-11.6

We will now configure postgresql,

# ./configure –with-systemd

Next, run the following commands,

# make

# make install

So these commands will install postgresql from the source. Next step would be to create a user for the postgres installation,

# useradd postgres

# passwd -d postgres

Next, we will also create a data directory for postgres,

# mkdir /usr/local/pgsql/data

# chown postgres /usr/local/pgsql/data

The next step would be to initialize the database. To do that, change the user to postgres & execute the following command,

# su – postgres

$ /usr/local/pgsql/bin/initdb -A trust -D /usr/local/pgsql/data

Once this is done, we can start the database but we need to create a service file to start the database. Revert back to superuser & create a service file,

# vi /etc/systemd/system/postgresql.service

[Unit]

Description=PostgreSQL database server

Documentation=man:postgres(1)

[Service]

Type=notify

User=postgres

ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data

ExecReload=/bin/kill -HUP \\\$MAINPID

KillMode=mixed

KillSignal=SIGINT

TimeoutSec=0

[Install]

WantedBy=multi-user.target

EOF

Save the file & exit. Then run the following command to start the database service,

# systemctl daemon-reload

# systemctl enable postgresql.service

# systemctl start postgresql.service

That’s it, the database is now ready to use.

Note: The Same method can be used to install other postgresql versions as well, the only change would download link for the version you wish to install.

We now end this tutorial on how to install postgresql from source, in our future tutorial we will discuss some other important Postgresql commands. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post Recommended guide to install POSTGRESQL from Source appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/recommended-guide-to-install-postgresql-from-source/feed/ 0 859
Simple guide to install POSTGRESQL on Centos/RHEL https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-centos-rhel/ https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-centos-rhel/#respond Sat, 28 Dec 2019 15:12:21 +0000 https://thelinuxgurus.com/?p=842 PostgreSQL (also called postgres) is a very famous, powerful, open-source relational database system that has been around for almost 30 years. It has a strong...

The post Simple guide to install POSTGRESQL on Centos/RHEL appeared first on The Linux GURUS.

]]>
PostgreSQL (also called postgres) is a very famous, powerful, open-source relational database system that has been around for almost 30 years. It has a strong reputation for reliability, performance & is feature robust. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users.

Recommended Read: How to setup SSH login without password

In this tutorial, we will learn to install PostgreSQL on CentOS & RHEL systems. So let’s start,


Install PostgreSQL on CentOS/RHEL

There are two methods using which we can install Postgresql on CentOS/RHEL systems,

1- Using the default CentOS repositories,

2- Using the official PostgreSQL repositories


1- Install Postgresql using default repositories

This method is quite simple, we can just run the following command to install postgresql on the server,

# yum install postgresql-server postgresql-contrib

Once the database has been started, we need to initialize the database before we can start using it,

# postgresql-setup initdb

Now we can start the database & also enable it to start at boot time,

# systemctl start postgresql

# systemctl enable postgresql

So our database is now up & ready to be used. The only downside to installing postgresql using this method is that we might not get the latest version as the default Centos or RHEL repositories don’t always maintain the latest versions.

So to install the desired version we must use the second method.


2- Install Postgresql using Official Postgresql repositories

Using this method, we will install a stable version 11 of PostgreSQL Install the official repository with the following command,

# yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm

When prompted, press ‘y’ to install. Next, we can run the yum install command to install postgresql,

# yum install postgresql11-server postgresql11

Next, we will initialize the DB,

# /usr/pgsql-11/bin/postgresql-11-setup initdb

Now we will start the database service & also enable it to start at boot time,

# systemctl start postgresql-11

# systemctl enable postgresql-11

PostgreSQL configuration


Set the Admin user password

To change the database admin user password, first switch to postgres user,

# su – postgres

Next, execute the following command from the terminal,

$ psql -c “alter user postgres with password ‘NEWPASSWORDhere'”


Create a new user & a new database

To create a new database, run the following command,

$ createuser new_user

$ createdb new_db -O new_user

$ grant all privileges on database new_db to new_user


Connect to a database

To connect to a database, use the following command,

$ psql -U new_user -h localhost -d new_db


Connecting to the database from a remote system

The above command will only work from the localhost but if we want to access the database from a remote host, we need to make some changes as by default we can’t connect to the database from the remote system.

To allow access from remote systems, open the following file,

# vi /var/lib/pgsql/11/data/postgresql.conf

& look for the following line,

listen_addresses =

& change to

listen_addresses = ‘*’

Next, we will make changes on another file,

# vi /var/lib/pgsql/11/data/pg_hba.conf

& add the following lines,

# Accept from anywhere

host all all 0.0.0.0/0 md5

# Accept from trusted subnet

host all all 10.10.0.0/16 md5

We need to restart the database server to implement the changes.

# systemctl restart postgresql-11

We now end this tutorial, please also see our tutorial on some of the important Postgresql commands that you should know. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post Simple guide to install POSTGRESQL on Centos/RHEL appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-centos-rhel/feed/ 0 842