2

I am a laravel user. I run my project with using homestead, and I am using porstgreSQL as my database. I run psql -U homestead -h localhost in my cmd, and then I put secret as my password. But I got an error like this

psql: FATAL: password authentication failed for user "homestead"

Jems
  • 1,666
  • 4
  • 20
  • 50
  • The password you need to enter is the password you have configured for your Laravel connection in your app. – Craig Ringer Oct 05 '17 at 02:53
  • `DB_HOST=127.0.0.1 DB_PORT=54320 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret` @CraigRinger – Jems Oct 05 '17 at 03:00
  • 1
    The notable difference here is the *port*. You haven't specified a port in your `psql` command and PostgreSQL's default port is `5432`. You must have multiple PostgreSQL instances. What if you add `-p 54320` to your command line? – Craig Ringer Oct 05 '17 at 03:06
  • yes, you're right. It thinks that homestead is using port `5432`, that's why it won't connect. But do you know how to implement postgreSQL in homestead? I can't migrate my database @CraigRinger – Jems Oct 05 '17 at 03:18
  • I have no idea what homestead, laravel, etc even are – Craig Ringer Oct 05 '17 at 04:17
  • 1
    I would suggest you `sudo su - postgres` connecting to psql as Os user: `psql -w` setting the password to secret for user `homestead` if such user exists and trying again... – Vao Tsun Oct 05 '17 at 06:11

2 Answers2

7

For command line, you can try psql -U homestead -W -h localhost, that will force the password prompt.

If this doesn't work, read on...

You might need to look into whether or not the user can access PostgreSQL under that username from the IP you are logging in from. For this, you need to look into the file /etc/postgresql/9.6/main/pg_hba.conf (keeping in mind that the 9.6 is the version, so your directory name might be something like 9.1 or 9.3). In that file, you'll be looking for a line that looks like this:

host all all 127.0.0.1/32 md5

That line states that the IP address 127.0.0.1 can log in via port mask 32 using md5 password hashing. If you need to log in as homestead from, say, port 12.34.56.78, you would need to add this line underneath:

host all homestead 12.34.56.78/32 md5

After making this adjustment, you need to run pg_ctl reload from the command line for the changes to take effect.

e_i_pi
  • 4,590
  • 4
  • 27
  • 45
  • where is the path that I should run `pc_ctl reload`? – Jems Oct 05 '17 at 02:34
  • Good question! :) Try `/usr/lib/postgresql/9.6/bin/` – e_i_pi Oct 05 '17 at 02:41
  • I got this error `pg_ctl: no database directory specified and environment variable PGDATA unset` – Jems Oct 05 '17 at 02:52
  • SO has answers to that problem in the following links: [Where does PostgreSQL store the database?](https://stackoverflow.com/questions/1137060/where-does-postgresql-store-the-database/32055424#32055424) and [How to restart PostgreSQL server on MacOS?](https://stackoverflow.com/questions/7990539/how-to-restart-postgresql-server-on-macos) (never mind that that is for MacOS, the top answer is a solution to finding the working directory) – e_i_pi Oct 05 '17 at 03:04
  • Thankyou ffor the reference. – Jems Oct 05 '17 at 03:22
  • Providing the `-h localhost` flag should be enough – ultrasamad Feb 12 '20 at 15:58
2

Login Postgresql using command line:

psql -U posgtres -p 5432

to show list of database:

\l

to connect database:

\c "database_name"

to show list of table in database:

\dt

or

\dt+

to show list data of table:

SELECT * FROM "table_name";
Jems
  • 1,666
  • 4
  • 20
  • 50