How to create user for a db in postgresql?

Cover Image for How to create user for a db in postgresql?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Create a User for a Database in PostgreSQL?

So you've installed PostgreSQL on your CentOS server, created a database, and now you're running into authentication issues when trying to connect from your PHP script. Don't worry, we've got you covered! In this guide, we'll walk you through the steps to create a new user and grant them permissions for a specific database.

Step 1: Accessing the PostgreSQL Shell

To begin, make sure you have access to the PostgreSQL shell. You mentioned that you've connected to the shell using the root user, so we'll assume you're already there. If not, verify your access before proceeding.

Step 2: Creating a New User

To create a new user, execute the following command in the PostgreSQL shell:

CREATE USER your_new_username WITH PASSWORD 'your_password';

Replace your_new_username with the desired username for the new user, and your_password with a strong password of your choice. Remember to keep your password secure and avoid using common or easily guessable passwords.

Step 3: Granting Permissions to the User

Now that you have a new user, it's time to grant them permissions for the specific database. In PostgreSQL, permissions are granted using the GRANT statement. Execute the following command:

GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_new_username;

Replace your_database_name with the name of the database you want to grant the user access to.

Step 4: Testing the Connection

With the new user and permissions set up, it's time to test the connection from your PHP script. Make sure you update your script's connection configuration to use the correct username, password, and database name.

$host = 'your_host'; // e.g., localhost
$port = 'your_port'; // e.g., 5432
$database = 'your_database_name';
$username = 'your_new_username';
$password = 'your_password';

$conn = new PDO("pgsql:host=$host;port=$port;dbname=$database", $username, $password);

Replace the placeholders (your_host, your_port, your_database_name, your_new_username, and your_password) with the actual values you're using.

Conclusion

By following these four simple steps, you should be able to create a new user and grant them the necessary permissions to access your PostgreSQL database. Remember to keep your password secure and ensure that your PHP script is using the correct credentials.

See? Creating a new user for a PostgreSQL database doesn't have to be complicated! Give it a try and let us know if you run into any issues. And don't forget to share this guide with others who might find it helpful. Happy coding! 🚀💻

Have any questions or need further assistance? Drop them in the comments below, and we'll be glad to help!


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello