Create database and users using PostgreSQL

Sep 19, 2017 PostgreSQL 中文版

Since 2016, PostgreSQL becomes popular, and more and more developers consider it as their first choice database. Not only it’s quite easy to use, but provides many useful programs and commands for users. Thus, this post is about how to utilize those programs and commands to easily create databases and users on PostgreSQL.

Create database user

To use programs and commands provided by PostgreSQL, you have to change current user to postgres. Once you switch to it, let’s create an user:

createuser [user-name]

If you already installed sudo, you can do this:

sudo -u postgres [user-name]

After the above step, we need to set the user’s password by executing this:

psql -c "alter user [user-name] with encrypted password '[password]';"

Create database

Let’s create a database:

createdb [database-name]

You can also do this if you’ve installed sudo:

sudo -u postgres createdb [database-name]

After that, give an user access to the database:

psql -c "grant all privileges on database [database-name] to [user-name];"

Now, the database is ready and we can start to build something. :)

You might also like:

If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)   © 2015-2019 zeckli, thanks to Jekyll and GitHub.