1

I'm trying to install PostgreSQL from source and script it for automatic installation.

Installing dependances, downloading and compiling PostgreSQL works good. But there are 3 commands that I need to run as Postgres User

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
/usr/local/pgsql/bin/createdb test

I saw this link but it doesn't work in my script here is the output :

Success. You can now start the database server using:

    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start

server starting
createdb: could not connect to database template1: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
admin@ip-172-31-27-106:~$ LOG:  database system was shut down at 2015-03-27 10:09:54 UTC
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

And the script :

sudo su postgres <<-'EOF'
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ start
/usr/local/pgsql/bin/createdb pumgrana
EOF

After that, I need to press enter and the server is running. My database is not created. It seems like the script tries to create the database then run the server but I'm not sure. Can someone help me?

1 Answer 1

1

There are a few things wrong with that script:

  • pg_ctl should get a -w argument, making sure it waits until PostgreSQL has started before exiting.

  • You don't have any error checking, so it'll just keep going if something doesn't work. At minimum you should use set -e at the start.

I also suggest using sudo rather than su, which is kind of obsolete these days. You never need sudo su, that's what sudo -u is for. Using sudo also makes it easier to pass environment variables in. So I'd write something like (untested):

sudo -u postgres PATH="/usr/local/pgsql/bin:$PATH" <<-'EOF'
set -e
initdb -D /usr/local/pgsql/data/
pg_ctl -D /usr/local/pgsql/data/ -w start
createdb pumgrana
EOF

You might want to pass PGPORT or some other relevant env vars into the script too.

Completely separately to this ... why? Why do this? If you're automating an install from source, why not just build a .deb or .rpm automatically instead, then install that?

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not an expert in Linux binaries and that kind of stuff. I didn't think about that creating a .deb file. I'm gonna give a try. Also, I'm trying the solution you gave me I'll tell you if this works. And thank you for your answer
Antoher question about building an .deb package. Do you mean install postgreSQL with .deb package, do my suff on my db and then make it an .deb?
I have no idea what you're trying to do, so I can't really say.
Your solution worked. Thank you ! I'm trying to automating a postgreSQL installation on an Amazon EC2 virtual server (I know there is RDS, but for some reason we cannot use it). I want a full automatic installation of my database, tables, ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.