top of page

psql

​

This is the terminal program you can run to interact with your PostgreSQL installation from the terminal. If you tried that and are already having problems getting access, read on.

​

Initial Configuration

​

If you installed postgres (shorter, easier to type nickname for PostgreSQL) one of the standard ways, it is common for a user named postgres to have been added to your system. Sometimes if you simply type psql you cannot interact with the program because you don't have permission. Often typing sudo -u postgres psql will successfully start and give you access to interact with the psql program. That command tells your system to run the psql command as the postgres user. You don't need to know the password of the postgres user to do that, you just need to enter the same password you would normally enter to run sudo commands. In fact, the postgres user on your system is often setup without a password, which is actually more secure, so you probably should not assign a password to the postgres user on your system. Keep in mind the postgres user and password on your system has nothing to do with the postgres user and password inside of PostgreSQL.

​

On some systems (Solus), you will need to enable and start the PostgreSQL service before you can use it or access it via psql as described above. If Solus is your operating system, you can do this via:

​

sudo systemctl enable postgresql.service
sudo systemctl start postgresql.service

​

Once you can successfully connect to the database using psql, you should set the password for the postgres user inside of PostgreSQL. This is different from changing the password for the postgres user on your system, and it is more secure to set them to have different passwords, although often the postgres user on your system is setup to not be usable via a password, which is even more secure. Either way, it is typically a good idea to change the postgres user password inside of PostgreSQL because the default password is often the same as the username and it is a good idea to change default passwords. While psql is running, run a query similar to this to set the password: ALTER USER postgres PASSWORD 'whatever password you want';

​

Connecting to PostgreSQL From a Local Application (local to the PostgreSQL installation)

​

If the application you are using provides a way to specify a postgres username and password, you should be able to simply provide it and the connection should work. Just make sure you are providing the password set for the PostgreSQL user and not the password set for the user on your system. See the previous section for how to set that password to whatever you want it to be. You might be tempted to modify the pg_hba.conf file to change how users are authenticated. While it certainly is a good idea to learn how this is configured on your system, it is often setup with secure defaults, and lowering the security is probably not a good idea if all you would need to do to avoid that is change the postgres password as explained above. The default location for the pg_hba.conf  file is in the "data" directory of your postgres installation. Sometimes it is located at /etc/postgresql/9.4/main/pg_hba.conf

(or postgres for short)

bottom of page