testing postgres connection in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting testing postgres connection in shell script
# 1  
Old 01-12-2011
testing postgres connection in shell script

I have a script that does db query in postgres. I'm trying to put in some error checking in the script. I keep running into the password prompt problem where when invalid credentials/or database is put it, it would prompt me for a password which hangs the script. Is there a way that I can check for this kind of failure?
I have pgpass setup so if the crendentials/db/host are correct i will connect perfectly.

for example I have this in my script (with the wrong db), when I run this, it will prompt me for password.

query=`psql -d wrongdatabase -U user -h dbhost -c "select * from whatever;"`

Instead of prompting me for password I would like the script to just exit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using shell scripting for making queries on postgres sql

I have a situation where I have a list of airplanes that make a series of flights. Therefore I am looking up the different flights that each airplane makes based on a postgres sql query: select flightid from plane where airplane='DELTAx' As a result I get a series of flight numbers... (0 Replies)
Discussion started by: JSNY
0 Replies

2. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

3. Shell Programming and Scripting

Script for removing Postgres

I made a script to remove Postgres if this is already installed on your system. I have a other script to install Postgres, so this script can be used before you going to install Postgres. Do you like this script? I would love to hear feedback. #!/bin/bash # #################################... (2 Replies)
Discussion started by: dannyvdberg
2 Replies

4. Shell Programming and Scripting

Backup-Script for Postgres

Hi, I'm working with postgres. Now I found a script to backup my databases. # #!/bin/bash # # # setup environment PG_PORT=5432 PG_HOME=/usr/lib/postgresql/8.3 PG_VAR=/backup PG_DATA=/backup/data PG_BACKUPS=/backup/backup PG_LOGS=/backup/logs ... (6 Replies)
Discussion started by: karl_ha
6 Replies

5. Shell Programming and Scripting

Testing connection with a script

I have a box that has to start dhcpd when a certain connection is down and stop the dhcpd when the connection is up again. I would appriciate any help making this script: From server A: ping server B if connection is ok then do nothing if connection is not ok then start dhcpd until... (8 Replies)
Discussion started by: KimJensen
8 Replies

6. Shell Programming and Scripting

testing a dhcp connection

Hi there, I have a computers that has a fixed address : terminal48:~# cat /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo... (1 Reply)
Discussion started by: chebarbudo
1 Replies

7. Shell Programming and Scripting

how will i connect postgres database from unix shell script

I have got the solution so just closing this issue. (3 Replies)
Discussion started by: jambesh
3 Replies

8. Solaris

Testing physical network connection

How do you test the physical network connection of an interface in solaris. I know that if you have an active connection and the cable gets yanked, you can look in the messages file to check for link failure messages. But is there any better way to see if you have good layer 2 connectivity? (1 Reply)
Discussion started by: tjlst15
1 Replies

9. Shell Programming and Scripting

Testing VPN Connection

Hi All: I need a script that can be timed to run every half hour to an hour to run a traceroute through a VPN to test that a connection is still up from a Win XP system. Which would be the best, C++ or Perl and what are some good resources to look at. (If anyone has a script to do this... (1 Reply)
Discussion started by: maxhewitt
1 Replies

10. Shell Programming and Scripting

Testing ssh connection from KSH script

Hi. I have a kornshell script that runs on a daily basis as a cron job. Part of what the script does is copy the folder contents from another server to the current server (server where KSH script is running). I have a scp command, as follows: scp $REMOTE_HOST:$REMOTE_FILE_DIR/* $TMP_DIR ... (8 Replies)
Discussion started by: dmilks
8 Replies
Login or Register to Ask a Question
DROPDB(1)						  PostgreSQL 9.2.7 Documentation						 DROPDB(1)

NAME
dropdb - remove a PostgreSQL database SYNOPSIS
dropdb [connection-option...] [option...] dbname DESCRIPTION
dropdb destroys an existing PostgreSQL database. The user who executes this command must be a database superuser or the owner of the database. dropdb is a wrapper around the SQL command DROP DATABASE (DROP_DATABASE(7)). There is no effective difference between dropping databases via this utility and via other methods for accessing the server. OPTIONS
dropdb accepts the following command-line arguments: dbname Specifies the name of the database to be removed. -e, --echo Echo the commands that dropdb generates and sends to the server. -i, --interactive Issues a verification prompt before doing anything destructive. -V, --version Print the dropdb version and exit. --if-exists Do not throw an error if the database does not exist. A notice is issued in this case. -?, --help Show help about dropdb command line arguments, and exit. dropdb also accepts the following command-line arguments for connection parameters: -h host, --host=host Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket. -p port, --port=port Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. -U username, --username=username User name to connect as. -w, --no-password Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. -W, --password Force dropdb to prompt for a password before connecting to a database. This option is never essential, since dropdb will automatically prompt for a password if the server demands password authentication. However, dropdb will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt. --maintenance-db=dbname Specifies the name of the database to connect to in order to drop the target database. If not specified, the postgres database will be used; if that does not exist (or is the database being dropped), template1 will be used. ENVIRONMENT
PGHOST, PGPORT, PGUSER Default connection parameters This utility, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see Section 31.14, "Environment Variables", in the documentation). DIAGNOSTICS
In case of difficulty, see DROP DATABASE (DROP_DATABASE(7)) and psql(1) for discussions of potential problems and error messages. The database server must be running at the targeted host. Also, any default connection settings and environment variables used by the libpq front-end library will apply. EXAMPLES
To destroy the database demo on the default database server: $ dropdb demo To destroy the database demo using the server on host eden, port 5000, with verification and a peek at the underlying command: $ dropdb -p 5000 -h eden -i -e demo Database "demo" will be permanently deleted. Are you sure? (y/n) y DROP DATABASE demo; SEE ALSO
createdb(1), DROP DATABASE (DROP_DATABASE(7)) PostgreSQL 9.2.7 2014-02-17 DROPDB(1)