postgresql create db script

6 views
Skip to first unread message

Carl Karsten

unread,
Jan 18, 2008, 3:26:33 AM1/18/08
to django...@googlegroups.com
I am trying to write a shell script that do whatever needs to be done between
sudo apt-get install postgresql python-psycopg
and
manage.py syncdb

for MySql, I use this to generate somehting I can pipe into the CLI:

print """
DROP DATABASE IF EXISTS %(db)s;
CREATE DATABASE %(db)s;
GRANT ALL
ON %(db)s.*
TO %(user)s
IDENTIFIED BY '%(pw)s'
with grant option;
""" % {
'db':settings.DATABASE_NAME,
'user':settings.DATABASE_USER,
'pw':settings.DATABASE_PASSWORD }

pipe that into the MySql CLI.

I can't figure out how to do it with psql without it prompting me for input.

I don't need it that fancy, just a script with everything hard coded would be
fine.

Carl K


Brett Parker

unread,
Jan 18, 2008, 3:38:57 AM1/18/08
to django...@googlegroups.com
On 18 Jan 02:26, Carl Karsten wrote:
>
> I am trying to write a shell script that do whatever needs to be done between
> sudo apt-get install postgresql python-psycopg
> and
> manage.py syncdb
>
> for MySql, I use this to generate somehting I can pipe into the CLI:
>
> print """
> DROP DATABASE IF EXISTS %(db)s;
> CREATE DATABASE %(db)s;
> GRANT ALL
> ON %(db)s.*
> TO %(user)s
> IDENTIFIED BY '%(pw)s'
> with grant option;
> """ % {
> 'db':settings.DATABASE_NAME,
> 'user':settings.DATABASE_USER,
> 'pw':settings.DATABASE_PASSWORD }
>
> pipe that into the MySql CLI.
>
> I can't figure out how to do it with psql without it prompting me for input.

psql will happily take piped input - you might need to edit
/etc/postgres/pg_hba.conf (or similar location) and you might need to
add your user - if it's prompting for a password then the only way I
know of getting round the password issue is to specify it as the
environmental variable PGPASSWD.

Hope that helps,
--
Brett Parker

Anton P. Linevich

unread,
Jan 18, 2008, 12:11:43 PM1/18/08
to django...@googlegroups.com

PG_USER="_postgresql"
PG_PASS="password"
DB_HOST="127.0.0.1"
PREFIX="/backups/pgsql/`date "+%d"`"
DB="db_name"

echo $YOURSQL | PGUSER="$PG_USER" PGPASSWORD="$PG_PASS" psql -h $DB_HOST $DB
or:
PGUSER="$PG_USER" PGPASSWORD="$PG_PASS" psql -h $DB_HOST $DB -C "$YOURSQL"

--
Anton P. Linevich

Reply all
Reply to author
Forward
0 new messages