Postgresql Create User and Database

I know, there are tons of posts on the internet showing you how to do this already. Most of them however are outdated and no longer seem to actually work.

Here’s what I found to be working best:

Create User

CREATE USER myuser WITH PASSWORD 'strong_password';

Yup, it’s still as simple as that. Nothing special here.

Create Database

CREATE DATABASE mydatabase;

Couldn’t be easier.

Grant User Permissions on Database

This is where most of the guides online fail. These guides tell you to just:

\connect mydatabase
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;

Which, however, isn’t enough for modern PostgreSQL versions. Instead, we need all this:

ALTER DATABASE mydatabase OWNER TO myuser;

\connect mydatabase

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
GRANT ALL PRIVILEGES ON SCHEMA public TO myuser;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO myuser;
« Back to main page