Shell Programming Connection Postgres Database (data Backup)

Source: Internet
Author: User
Tags postgres database

The first step: Connect to the Linux service via Xshell or other tools,

Step Two: Create a script: Touch se.sh

Step three: Enter I to enter the following command on behalf of the start input:

The script is as follows: (SQL statement can be any complex SQL, simple to write here)

! /bin/sh
Psql-h 127.0.0.1-u postgres-w-D db_test >/root/zyy <<-eof
SELECT * from Test;
Eof

Script parsing:

-H represents the IP of the PC to which the postgres is connected, meaning to connect the local database 127.0.0.1 local, or to enter a different IP

-D represents the database name of the connection

The >/ROOT/ZYY representative writes the content to the Zyy file.

EOF is used in conjunction with << to represent the input of subsequent inputs as subcommands or sub-shells until EOF is encountered and then returned to the keynote shell.

Run the SH script:./se.sh, enter the password for the database user

Output Result:

View Result: Cat Zyy

Shell Programming Connection Postgres Database (data Backup)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.