The error FATAL: database "dbname" does not exist
in PostgreSQL indicates that the client is attempting to connect to a database named “dbname” that does not exist on the PostgreSQL server instance. Here are the steps to diagnose and fix this issue:
Diagnose
- Check Database Name: Verify that the database name you are trying to connect to is spelled correctly, including the correct case, as PostgreSQL database names are case-sensitive.
- List Databases: List all databases on your PostgreSQL server to see if the database you are trying to connect to exists. You can do this by connecting to PostgreSQL with a superuser or a user with the necessary privileges and running the
\l
or\list
command in the psql command-line interface. - Check Connection Parameters: If you are using a connection string or a database URL, ensure that the database name is specified correctly in the connection parameters.
- Verify User Permissions: Ensure that the user has permission to access the database. Even if the database exists, if the user doesn’t have the CONNECT privilege, the error might still occur.
Fix
Once you’ve diagnosed the problem, here are the possible fixes:
- If the database name is incorrect, connect to the correct database name.
- If the database does not exist, you can create it using the following SQL command:
CREATE DATABASE dbname;
Make sure to replace “dbname” with the correct name of the database you want to create. This command should be executed by a user with the necessary privileges, typically a superuser like postgres
.
- If the issue is with the connection parameters, correct the database name in your application’s database configuration or in the connection string.
- If the user does not have permission to connect to the database, a superuser can grant the permission using the following SQL command:
GRANT CONNECT ON DATABASE dbname TO username;
Replace “dbname” with the name of the database and “username” with the name of the user that needs access.
After applying the appropriate fix, try reconnecting to the database to verify that the issue has been resolved. If you continue to experience problems, double-check the database name, the existence of the database, and the user permissions.