How to run postgresql from Command line and know available Databases in Windows - postgresql

I am very new to postgreSQL and struggling to create a connection in my Eclipese.
Here is what I have done till now:
Installed DBeaver plugin for eclipse from marketplace.
Installed PostgreSQL version 10 from https://www.postgresql.org/download/windows/
Now I attempt to create a connection in Eclipse but I get an error "FATAL: password authentication failed for user "testuser"
I checked through a similar thread (
Getting authentication failed error with postgresql from command line
) but could not resolve the issue.
Could you please help.

To connect the PostgreSQL via Command line you have to install a PostgreSQL provided tool called "psql" if not installed already.
Then use the below command to connect to PostgreSQL database.
psql -h localhost -p 5432 -U postgres
Explanation:
localhost: is the hostname of machine where PostgreSQL is installed.
5432 : is the default PostgreSQL port
postgres : is the username of to connect to DB
Solution for your issue:
FATAL: password authentication failed for user "testuser"
As error message specified, either user not exists on database or the password that you supplied to connect is incorrect. Re-verify your user and password, and try again.
All the best.

Related

Unable to execute postgres command

I'm having difficulty changing the password associated with the postgres user after installing postgres on my windows 10 machine. My apologies in advance as I'm quite unfamiliar with postgres as well as the commands required to work with it.
I've referenced the approved answer in the below article:
FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)
I'm stuck on the step that requires me to
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
I don't quite understand this step. I've taken the following steps
Open cmd
run psql
The system then asks me for password for username jason. Regardless of what I enter, i get the following message:
psql: error: could not connect to server: FATAL: password authentication failed for user "jason"
At no point do I have an opportunity to enter the following command:
ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
How can I run this command without being asked to enter a password for postgres?
Thanks!
The steps below require that you remember what you did when you installed PostgreSQL.
Locate the data directory where the installation process created the database cluster. By default, that would be a subdirectory of where you installed the software (which is a bad place)
Edit the pg_hba.conf file therein and add this line on top:
host postgres postgres 127.0.0.1/32 trust
Reload or restart PostgreSQL.
Start cmd.exe and enter
psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
If psql is not on your PATH, use the absolute path C:\...\psql.
Use \password to change the password.

Can't connect to postgres via PSQL or php, only pgAdmin4

I have PostgreSQL 12 installed and using Windows 10. My goal is to connect to postgre via php, because it didn't work I tried with plsql but it didn't work either, i always get (with both plsql and php using PDO):
FATAL: password authentication failed for user "postgres"
Connection via plsql in cmd
However, when trying via pgAdmin4, I can connect and access my databases. I first use my root password then sometime my postgres user one, so I'm sure my password works.
Here is my pg_hba.conf file:
pg_hba.conf file
I have a set password for postgres which was set vua pgAdmin4.
I don't understand why it would work one way and not the other, can anyone help me?
So after trying several things, I found that plsql and php error was due to the wrong port being adressed.
By default 5432 is used, but for some reason my configuration was using 5433.
You can find this information in pgAdmin4 by opening PostgreSQL 12 properties, and then checking in connection tab.
To change the used port in plsql I used:
psql -U postgres -p 5433
Change 5433 by your configured port if you have same issue, you also need to specify the port if using php's PDO.

pgAdmin III : No servers showing up

I've followed many online tutorials on how to install pgAdmin correctly, I've installed and uninstalled it multiple times but whenever install it. It shows Server Groups without any servers in it.
Then when I try and register a new server using localhost for both name and host I get this error:
server doesn't listen
Searched for days trying to find a solution for this, any ideas?
Happened with me as well, when I started pgadmin3 for the first time, I was expecting it to ask for the password which I provided during but it didnt ask and then didnt display any servers.
Anyways, you can create a server yourself.
If your server is on the same machine,
provide a name: test
provide server: localhost
provide a port number : 5432 (default port)
provider a username: postgres (default user, you can change later in login roles)
password: provide something
Restart if required, and it will then show you a
database with postgres (default db),
tablespace, and login roles under your server, and you should be good to go .
First Create a Server with the following credential-
Host Name/address : localhost
Port : 5432
Maintenance database : postgres
Username : postgres
Password : *Your_Password*
Note : Before Creating the Server first start the postgresql server
Reinstalled program and saved it to directory C:\ instead of C:\program_Files
When I did this invalid password message appeared. So I had to change postgres user password:
In the terminal window:
Switch to the postgres account on the server by typing:
sudo -i -u postgres
Now I can access a Postgres prompt immediately by:
psql
Change the postgres user password:
ALTER USER postgres WITH PASSWORD 'newpass';

Openbravo postgres installation

I am trying to get a copy of Openbravo on Ubuntu for development. I am following the official guide and I get stuck by a Postgres error.
I have successfully downloaded the source code through the mercurial commands. ant setup also worked for me.
When I run the Wizard I write this information.
The problem comes when I run ant install.source:
BUILD FAILED
/home/User/openbravo/build.xml:734: The following error occurred while executing this line:
/home/User/openbravo/src-db/database/build-create.xml:50: The following error occurred while executing this line:
/home/marcguilera/openbravo/src-db/database/build-create.xml:77: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
The stacktrace is longer.
I)
First we need to change the PostgreSQL postgres user password else we will not be able to access the server. As the “postgres” Linux user, we will execute the psql commands below.
In a terminal, type:
$ sudo -u postgres psql postgres
Set a password for the "postgres" database role using the command:
\password postgres
II) Create database
To create the first database, which we will call leo, simply type:
sudo -u postgres createdb leo
III) install Server Instrumentation (for PgAdmin) for Postgresql 9.1
PgAdmin requires the installation of an add-on for full functionality. The "adminpack" addon, which it calls Server Instrumentation, is part of postgresql-contrib, so you must install that package.
Then to activate the extension,
For "Postgresql 9.1"+ install the adminpack "extension" in the "postgres" database:
$ sudo -u postgres psql
then
CREATE EXTENSION adminpack;
IV) Using pgAdmin III GUI
To get an idea of what PostgreSQL can do, you may start by firing up a graphical client. In a terminal type :
pgadmin3
You will be presented with the pgAdmin III interface.
* Click on the "Add a connection to a server" button (top left).
In the new dialog:
enter the address **127.0.0.1**
host is **jdbc:postgresql://localhost:5432/postgres**
the default database ("leo" in the example above)
your username ("postgres") and your password.
One more step is required in order to allow pgAdmin III to connect to the server, and that is to edit pg_hba.conf file and change the authentication method from peer to md5 (Will not work if you have not set the password.):
sudo nano /etc/postgresql/9.1/main/pg_hba.conf
and change the line
# Database administrative login by Unix domain socket
local all postgres peer
to
# Database administrative login by Unix domain socket
local all postgres md5
Now you should reload the server configuration changes and connect pgAdmin III to your PostgreSQL database server.
sudo /etc/init.d/postgresql reload
With this GUI you may start creating and managing databases, query the database, execute SQl etc.
https://help.ubuntu.com/community/PostgreSQL
v) Run openbravo with postgres
Just adjust the db name, user name and the passwords.
Hope that helps.

How to connect pgAdmin III (Windows) to postgresql (Linux in virtual box)

I have a postgresql demon on my virtual Linux box, and just installed pgAdmin III on the Windows machine. Now I'd like to configure the first connection.
What I did so far:
I edited /etc/postgresql/9.1/main/pg_hba.conf and added a line to make the demon accept connections from my Windows machine:
host all all 192.168.123.45/32 md5
I set a shell passphrase for the system user postgres (sudo passwd postgres)
Edit: this seems to be irrelevant
I copied /usr/share/postgresql/9.1/pg_service.conf.sample to /etc/postgresql-common/pg_service.conf and uncommented the sample service, giving me
[postgres]
dbname=postgres
user=postgres
Edit: this seems to be irrelevant
I put a line into /etc/environment to point to the pgservice.conf file:
PGSERVICEFILE=/etc/postgresql-common/pg_service.conf
(which works; when I duplicated the PuTTY session, it had this variable)
I restarted the database server after each change:
sudo /etc/init.d/postgresql restart
Finally, I restartet the Linux box,
The server seems to have restarted successfully:
[ ok ] Restarting PostgreSQL 9.1 database server: main.
However, when I try to connect to the service, pgAdmin III gives me an error message:
Error connecting to the server: definition of service "postgres" not found.
dmesg | grep postgres gives me a single line:
[ 18.054965] postgres (2242): /proc/2242/oom_adj is deprecated, please use /proc/2242/oom_score_adj instead.
I have no clue whether postgresql uses my pq_service.conf at all, or whatever else is the problem; any help is appreciated ...
Edit:
My original question, "How can I know whether postgresql uses my pg_service.conf file?", seems to be answered - it simply doesn't. I still can't connect; but now the question doesn't match the error messages anymore.
I removed the "Service" entry from my "New Server Registration" data. Now I get another error - something like "password authentication for user >>postgres<< failed". I'm quite sure the password is correct - I just set it ... and I tested it by commenting out my ssh key from the authorized_keys file.
I'd happily connect with my ssh key, but this seems to be difficult as well. With PuTTY, my key is taken from Pageant, and I'm logged in without any problem; pgAdmin talks about "SSH tunnelling", but I normally don't need tunnels for this local machine ...
I tried to create a tunnel anyway.
PuTTY session:
Source port is 5432
destination is my-vbox.host.name:5432
In pgAdmin, "SSH Tunnel":
Username postgres
using Identity file
Tunnel host localhost → error:
"SSH error: Could not connect to socket with error code 10051"
Tunnel host localhost:5432 → error:
"SSH error: Unable to resolve host: localhost:5432"
Tunnel host 127.0.0.1:5432 → error:
"SSH error: Unable to resolve host: 127.0.0.1:5432"
How the heck is this supposed to be configured?!
I finally managed to connect as db user tobias (I never had problems to connect locally, with psql).
# sudo -u postgres psql postgres
psql (9.1.9)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
tobias | Superuser | {}
postgres=# ALTER USER tobias WITH PASSWORD 's3cr3t';
ALTER ROLE
After doing this, I could connect as tobias, using the password just set.
The "Maintenance DB" happens to be template1; I didn't try to change this.
For further reference - I activated log_connections in /etc/postgresql/9.1/main/postgresql.conf and watched the log:
sudo tail -f /var/log/postgresql/postgresql-9.1-main.log