Skip to content

Create a PostgreSQL database

Fill in the variables above and set the user’s password in the script:

init.sql
CREATE USER ${{ USERNAME }} WITH PASSWORD 'put_your_password';
CREATE DATABASE ${{ DBNAME }} OWNER ${{ USERNAME }};
GRANT ALL PRIVILEGES ON DATABASE ${{ DBNAME }} TO ${{ USERNAME }};
\c ${{ DBNAME }}
GRANT ALL ON SCHEMA public TO ${{ USERNAME }};

Run as superuser (e.g. postgres):

Terminal window
psql -h ${{ HOSTNAME }} -p 5432 -U postgres -f init.sql

Or inline. Use a separate -c per statement: psql sends each -c string to the server as one request, so two statements in a single -c run inside one implicit transaction — and CREATE DATABASE cannot run inside a transaction block.

Terminal window
psql -h ${{ HOSTNAME }} -p 5432 -U postgres \
-c "CREATE USER ${{ USERNAME }} WITH PASSWORD 'put_your_password';" \
-c "CREATE DATABASE ${{ DBNAME }} OWNER ${{ USERNAME }};"