0

I have a shell script which I pass RDS postgres host into, within the script I can connect to postgres db via command below within script. How can I pass psql commands into the shell script to verify a table ? or list all db ? like

\dt 
\l

psql "host=$RDSHOST port=5432 sslmode=disable dbname=mydbname user=testuser password=$PASSWORD"

I get the prompt to DB no issue, would like to pass some commands to verify a db exists or list a table and exit the shell script with its output.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
DeclanG
  • 240
  • 2
  • 6
  • 21
  • something similar to $ mysql -h myhost.rds.amazonaws.com -u user -D my_database -p --batch --quick -e "SELECT * FROM my_table" > output.csv – DeclanG Feb 15 '19 at 09:20
  • was able to get with psql -h $RDSHOST -p 5432 sslmode=disable -d mydbname -U testuser -c 'SELECT * FROM pg_catalog.pg_tables' – DeclanG Feb 15 '19 at 09:30
  • This works from pgadmin , SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'myschema' \ but I cannot get to run from cli, returns empty – DeclanG Feb 15 '19 at 13:28
  • I can also list using \d myschema.accounts, cannot seem to read the table though – DeclanG Feb 15 '19 at 14:31

1 Answers1

0

From Run PostgreSQL queries from the command line:

psql -U username -d mydatabase -c 'SELECT * FROM mytable'
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Thanks John, I can do this ok when logged in, what im trying to do is run the psql command within the shell script after login to aws rds instance or add to existing connestion string , if I try add this command to script it just stops after login to db until I manully exit, if I try add the -c option to the connection string "psql "host=$RDSHOST port=5432 sslmode=disable dbname=mydbname user=testuser password=$PASSWORD" it complains it does not recognize the -c option – DeclanG Feb 15 '19 at 09:09
  • John, You pointed me in the right direction thanks , psql -h $RDSHOST -p 5432 sslmode=disable -d mydbname -U testuser -c 'SELECT * FROM pg_catalog.pg_tables' – DeclanG Feb 15 '19 at 09:31