Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6702157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:01:19+00:00 2026-05-26T07:01:19+00:00

I am a novice bash script user. I am trying to execute this postgresql

  • 0

I am a novice bash script user. I am trying to execute this postgresql command (which outputs commands that drops tables whose name is like r_395)

SELECT 'DROP TABLE ' || tablename FROM pg_catalog.pg_tables where tablename like 'r_395%';

output of this query is:

?column?      
--------------------
 DROP TABLE r_395_0
 DROP TABLE r_395_1
 DROP TABLE r_395_2
 DROP TABLE r_395_3
 DROP TABLE r_395_4
 DROP TABLE r_395_5
 DROP TABLE r_395_6
 DROP TABLE r_395_7
 DROP TABLE r_395_8
 DROP TABLE r_395_9
(10 rows)

using bash with this command:

/bin/su - postgres -c "/usr/bin/psql database -c SELECT 'DROP TABLE ' || tablename FROM pg_catalog.pg_tables where tablename like 'r_395%'" > droptables

But I am getting this error:

psql: FATAL:  role "DROP TABLE " does not exist
-bash: tablename: command not found

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:01:19+00:00Added an answer on May 26, 2026 at 7:01 am

    Probably due to the quotes being reinterpreted as you’re including a -c. You need something like:

    su - postgres -c "/usr/bin/psql database -c \"SELECT 'DROP TABLE ' || ....
    

    i.e. you need to quote the arg to psql’s -c option again.

    This rapidly turns into a mess. I suggest you instead get the command you want psql to execute produced as output and then just have psql execute stdin (by not passing -c at all). This avoids the quoting hell, and makes testing easier (just take out the pipe to psql and you see what it will be getting). That is:

    echo "SELECT 'DROP TABLE ' || tablename ... " | su - postgres -c "psql database"
    

    or use a “here document”:

    su - postgres -c "psql database" <<EOF
    SELECT 'DROP TABLE ' || tablename ...
    EOF
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have bash script running a wget command to get a directory: wget
Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once db_login.php
I am a novice programmer who is trying to teach myself to code, specifically
I'm a very novice OCaml programmer so please forgive me if this is a
I'm a Python novice, trying to use pyCurl. The project I am working on
I am a Delphi novice, but I'm trying to understand the relationship between TApplication
When running a bash script on ubuntu 9.10, I get different behavior from bash
I use the following Perl syntax in my bash script: perl -i –pe 'next
I'm pretty much a novice to shell scripting. I'm trying to send the output
Novice question I guess: The Scenario: I have an asp.net page that contains 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.