The post Beginner’s guide to Backup Postgres Database appeared first on The Linux GURUS.
]]>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
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.
]]>The post Recommended guide to install POSTGRESQL from Source appeared first on The Linux GURUS.
]]>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.
]]>The post Simple guide to install POSTGRESQL on Ubuntu appeared first on The Linux GURUS.
]]>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,
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
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.
]]>The post Simple guide to install POSTGRESQL on Centos/RHEL appeared first on The Linux GURUS.
]]>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,
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
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.
]]>