postgresql Archives - The Linux GURUS https://thelinuxgurus.com/tag/postgresql/ 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 postgresql Archives - The Linux GURUS https://thelinuxgurus.com/tag/postgresql/ 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
Important PostgreSQL commands that every beginner should know https://thelinuxgurus.com/important-postgresql-commands-that-every-beginner-should-know/ https://thelinuxgurus.com/important-postgresql-commands-that-every-beginner-should-know/#respond Tue, 03 Mar 2020 06:05:03 +0000 https://thelinuxgurus.com/?p=894 PostgreSQL is one of the most widely used databases in the world & is also very easy to administer. In this tutorial, we will learn...

The post Important PostgreSQL commands that every beginner should know appeared first on The Linux GURUS.

]]>
PostgreSQL is one of the most widely used databases in the world & is also very easy to administer. In this tutorial, we will learn some important postgresql commands that every beginner should know.

In our previous tutorials, we have learned to install postgresql on CentOS/RHEL, on Ubuntu & also learned to install Postgresql from source. So if you need to install postgresql first, please refer to those tutorials.

Recommended Read: Scheduling CRON Jobs with Crontab for Beginners

Also Read: Beginner’s guide to Backup Postgres Database


PostgreSQL commands


1- Connect to the database

Connection from localhost,

# su – postgres

Now to connect to the database, we will run the command ‘psql’,

$ psql

Once into the database, we can use ‘\q’ to exit the database,

postgres=# \q

Connecting from Remote server,

# psql -h host_ip -U user_name -p port_number -d database_name


2- Create role/user

To create a role, first connect to database & then we will use command ‘createuser’,

postgres=# CREATE USER test;

Or we can also use the following,

postgres=# CREATE ROLE test;

To create a user with password,

$ CREATE USER test PASSWORD ‘enter password here’


3- Delete a role/user

To delete a role, we will use the DROP command,

postgres=# DROP ROLE test;


4- Show list of users (List of roles)

postgres=# \du


5- Create a new Database

To create a new database, use the following,

postgres=# CREATE DATABASE thelinuxgurus;


6- Delete a database

To delete a created database, use

postgres=# DROP DATABSE thelinuxgurus;


7- Show list of databases

postgres=# \l


8- Change DB when you are connected to other DB

postgres=# \c thelinuxgurus;

Or we can also user the following command,

postgres-# \connect new_database


9- Create a table

To create a table, connect to the desired database & create a table with the following command,

thelinuxgurus=> CREATE TABLE USERS (Serial_No int, First_Name varchar, Last_Name varchar);

Now insert some records into it,

thelinuxgurus=> INSERT INTO USERS VALUES (1, ‘Dan’, ‘Prince’);


10- Delete a table

To delete a table from the database, we will use,

thelinuxgurus=> DROP TABLE USERS;


11- List tables

postgres=# \dt


12- Describe a table to show its structure

postgres=# \d table_name


13- Adding a column to a table

Once a table has been created, it can be altered to add new columns to it using the following command,

thelinuxgurus=> ALTER TABLE USERS ADD date_of_birth date;


14- Updating a Row

We can not only alter the columns of a table but can also update the records that are entered in rows,

thelinuxgurus=> UPDATE USERS SET date_of_birth = ‘03-04-1990’ WHERE Seriel_No = ‘1’;

thelinuxgurus=> SELECT * FROM USERS;

The last command was to verify the changes that were made.


15- Remove a Column

To remove a column from a table, run

thelinuxgurus=> ALTER TABLE USERS DROP date_of_birth;


16- Remove a Row

To delete a row, use the following example,,

thelinuxgurus=> DELETE FROM USERS WHERE Seriel_No = ‘1’;


17- List of all functions

To check all the functions on the database, use

postgres=# \df


18- To edit function

To edit a function, use the following command,

postgres=# \ef function_name


19- List of all schema in DB

To see all the schemas on the database, use

postgres=# \dn


20- To check the current Installed version

postgres=# select version();


21- To display the command history

postgres=# \s


22- Execute psql commands using a file

So if we have a file (like a .sql file) & we need to execute the file into our database, then use the following command

postgres=# \i /path/file_name


23- To turn on query execution time

postgres=# \timing


24) To list database views

postgres=# \dv


25) Check current date

postgres=# select now();

These were some of the important PostgreSQL commands that everyone should know about to maintain & administer the PostgreSQL database. Please feel free to send in any question 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 Important PostgreSQL commands that every beginner should know appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/important-postgresql-commands-that-every-beginner-should-know/feed/ 0 894
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 Ubuntu https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-ubuntu/ https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-ubuntu/#respond Sat, 28 Dec 2019 15:27:54 +0000 https://thelinuxgurus.com/?p=846 PostgreSQL (also called postgres) is very famous, powerful, open-source object-relational database management system that has been around for almost 30 years. PostgreSQL requires very minimum...

The post Simple guide to install POSTGRESQL on Ubuntu appeared first on The Linux GURUS.

]]>
PostgreSQL (also called postgres) is 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 Centos/RHEL

Also Read: How to setup SSH login without password

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


Install PostgreSQL on Ubuntu

There are two methods using which we can install Postgresql on Ubuntu systems,

1- Using the default Ubuntu repositories,

2- Using the official PostgreSQL repositories


1- Install Postgresql using default repositories

Ubuntu also maintains Postgresql on official Ubuntu repositories but they might be an older version. So if you don’t have any concern for the Postgres version, then use this method & if you want to install a particular version, then the second method is the way to go.

This method is quite simple, to install PostgreSQL  execute the following command,

$ sudo apt-get install postgresql postgresql-contrib

Once the database has been installed, we can start the database & also enable it to start at boot time,

# sudo systemctl start postgresql

# sudo systemctl enable postgresql

So our database is now up & ready to be used.


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,

$ wget –quiet -O – https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add –

Next, execute the following command for add repository contents,

$ RELEASE=$(lsb_release -cs)

$ echo “deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}”-pgdg main | sudo tee  /etc/apt/sources.list.d/pgdg.list

Now to install Postgresql on Ubuntu, we will run the following commands,

$ sudo apt update

$ sudo apt -y install postgresql-11

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

# sudo systemctl start postgresql-11

# sudo 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 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 a remote system.

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

$ sudo vi /etc/postgresql/11/main/postgresql.conf

& look for the following line,

listen_addresses =

& change to

listen_addresses = ‘*’

Next, we will make changes on another file,

$ sudo  vi /etc/postgresql/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.

# sudo systemctl restart postgresql-11

We now end this tutorial on how to install postgresql on Ubuntu, 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 Ubuntu appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/simple-guide-to-install-postgresql-on-ubuntu/feed/ 0 846