24
loading...
This website collects cookies to deliver better user experience
pg_database
database using SELECT datname FROM pg_database WHERE datname LIKE 'test%' AND datistemplate=false
SQL query. The WHERE clause in the SQL query will restrict the database names to my test databases and non-template databases.DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT datname
FROM pg_database
WHERE datname LIKE 'test%' AND datistemplate=false
LOOP
EXECUTE 'DROP DATABASE ' || quote_ident(r.datname) || ';' ;
END LOOP;
END $$;
DROP DATABASE cannot be executed from a function
CONTEXT: SQL statement "DROP DATABASE test_01024da6a3;"
PL/pgSQL function inline_code_block line 12 at EXECUTE
DROP DATABASE
SQL query and run the query using the \gexec
meta-command.psql -d postgres
command from a terminal window. If you don't specify the user name, PostgreSQL will default to the current operating system user. The default user has permission to create and drop databases. You can connect as a different user by appending the -U command-line option to the psql
command (psql -U username -d postgres
).psql
prompt you get will either be postgres=>
or postgres=#
if you are a database superuser. At the prompt, type your SQL query to construct DROP DATABASE
statements for the databases you want to drop. Do not terminate the query with a semicolon (;) --SELECT 'DROP DATABASE ' || quote_ident(datname) || ';' FROM pg_database WHERE datname LIKE 'test%' AND datistemplate=false
. If you terminate the query with a semicolon (;), the query will run when you press the RETURN key.\gexec
meta-command sends the current query buffer to the server and treats each column of each row of the query's output as an SQL statement to be executed. In our case, our query will output a DROP DATABASE SQL query for each test database. \gexec
meta-command will then run each statement from the output.postgres=#
postgres=# SELECT 'DROP DATABASE ' || quote_ident(datname) || ';'
postgres=# FROM pg_database
postgres=# WHERE datname LIKE 'test%' AND datistemplate=false
postgres=#
postgres=# \gexec
#!/bin/bash
test_databases_file=~/projects/test_dbs.txt
psql -d postgres -c "COPY (SELECT datname FROM pg_database WHERE datname LIKE 'test%' AND datistemplate=false) TO '$test_databases_file'"
while read dbname
do
echo "dropping DB $dbname..."
dropdb "$dbname"
done < $test_databases_file
echo "removing $test_databases_file file"
rm $test_databases_file
#!/bin/bash
is called a shebang or a bang line. It specifies the interpreter that will execute the commands.chmod +x drop-test-dbs.sh
)../drop-test-dbs.sh
). If the script file is not within the current folder, remember to include the file path (file-location/drop-test-dbs.sh
).psql
is a powerful command-line tool that ships with PostgreSQL. You can use psql
to connect to a server and interact with databases. Using psql
and querying the pg_database
database for the required databases together with \gexec
meta-command is another option for dropping multiple databases in PostgreSQL. However, failing to filter for required databases will drop all databases from your server.psql
commands into a shell script file and execute the script file.#!/bin/bash
for n in {1..15}
do
# Use Bash command substitution to store the
# output (GUID) from uuidgen command
DB_ID=$(uuidgen)
# Use Bash Substring expansion to extract
# 8 characters of the GUID starting from index 0
db="test_db_${DB_ID:0:8}"
createdb "$db"
echo "DB $db created"
done