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)