This script will manage a user-run postgres database. The main idea here is to run Postgres on a system where you don't have root access (and don't want root access). If you can not install Postgres through the system (or docker), then this is one way to still be able to use the database, even if you can't start it as a service.
-
Create a database and initialize it with a user login
./db.sh initdb dbname username userpass
-
Start/stop the database
./db.sh start|stop
-
Connect to database
./db.sh psql dbname {-U myuser -W}
This works by creating a Postgres database in the main directory of this script in data/
. A run/
directory is also created to store log files and the unix socket. Connections are then made via this unix socket, so you don't have to open a TCP socket.
This isn't the best way of doing this with usernames and passwords sent in via command line arguments. If you want a more secure method, think of this as a place to get started.
But, the database itself will be owned by the user (not the system postgres user). The unix socket used to connect to the database also will belong to that user. Because of this, you can use Postgres on a multi-user system, start and stop the db at will, and keep the connections secure.