Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to execute pgsql commands in files

I am trying to automate a set of procedures that create TEMPLATE databases.

I have a set of files (file1, file2, ... fileN), each of which contains a set of pgsql commands required for creating a TEMPLATE database.

The contents of the file (createdbtemplate1.sql) looks roughly like this:

CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8';  \c mytemplate1  CREATE TABLE first_table (   --- fields here .. );  -- Add C language extension + functions \i db_funcs.sql 

I want to be able to write a shell script that will execute the commands in the file, so that I can write a script like this:

# run commands to create TEMPLATE db mytemplate1 # ./groksqlcommands.sh createdbtemplate1.sql  for dbname in foo foofoo foobar barbar do     # Need to simply create a database based on an existing template in this script     psql CREATE DATABASE $dbname TEMPLATE mytemplate1 done 

Any suggestions on how to do this? (As you may have guessed, I'm a shell scripting newbie.)

Edit

To clarify the question further, I want to know:

  1. How to write groksqlcommands.sh (a bash script that will run a set of pgsql cmds from file)
  2. How to create a database based on an existing template at the command line
like image 378
Homunculus Reticulli Avatar asked Dec 21 '11 18:12

Homunculus Reticulli


People also ask

How do I run a PostgreSQL file?

Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL shell. Open the SQL shell from the menu bar of Windows 10. Add your server name, database name where you want to import the file, the port number you are currently active on, PostgreSQL username, and password to start using SQL shell.

How do I run a DDL script in PostgreSQL?

To run DDL in the form of an SQL script, you should use psql , the same command you use to connect to the server interactively. I recommend using ON_ERROR_STOP=1 and -1 , so it runs the DDL in a single transaction and aborts on any error.


1 Answers

First off, do not mix psql meta-commands and SQL commands. These are separate sets of commands. There are tricks to combine those (using the psql meta-commands \o and \\ and piping strings to psql in the shell), but that gets confusing quickly.

  • Make your files contain only SQL commands.
  • Do not include the CREATE DATABASE statement in the SQL files. Create the db separately, you have multiple files you want to execute in the same template db.

Assuming you are operating as OS user postgres and use the DB role postgres as (default) Postgres superuser, all databases are in the same DB cluster on the default port 5432 and the role postgres has password-less access due to an IDENT setting in pg_hba.conf - a default setup.

psql postgres -c "CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8'                   TEMPLATE template0" 

I based the new template database on the default system template database template0. Basics in the manual here.

Your questions

How to (...) run a set of pgsql cmds from file

Try:

psql mytemplate1 -f file 

Example script file for batch of files in a directory:

#! /bin/sh  for file in /path/to/files/*; do     psql mytemplate1 -f "$file" done 

The command option -f makes psql execute SQL commands in a file.

How to create a database based on an existing template at the command line

psql -c 'CREATE DATABASE my_db TEMPLATE mytemplate1' 

The command option -c makes psql execute a single SQL command string. Can be multiple commands, terminated by ; - will be executed in one transaction and only the result of the last command returned.
Read about psql command options in the manual.

If you don't provide a database to connect to, psql will connect to the default maintenance database named "postgres". In the second answer it is irrelevant which database we connect to.

like image 197
Erwin Brandstetter Avatar answered Sep 22 '22 15:09

Erwin Brandstetter