Distribution, Installation, and Connectivity of PostgreSQL - postgresql

Have not been able to find an answer to this yet, but if this a duplicate, please mark accordingly!
Trying to understand how PostgreSQL can be distributed to an end-user via silent installation and the respective actions if:
PostgreSQL is already installed on the computer system
How to connect to PostgreSQL as a superuser, if it is already installed
Create a database (relating to #2, since we would not know the credentials of postgresql user when PostgreSQL was initially installed)
For #1, from my research and understanding, there are two methods:
Determining if a postgresql-[version] service is installed (per this QA)
Determining if HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\ registry exists, and if it does, seeing the versions and service names under the \Installations\ and \Services\ subs
However, I am more concerned about the connectivity part. If my application (to be written in C#) is dependent on a database name "MyProgram," how would it be possible to create a database in PostgreSQL and with what credentials?
From my understanding, the way to go would be to attempt to log in as the default superuser, postgres, to the default database, postgres, and create a new user and database from that connection. But, the password for postgres user is set during by the user/program that is initially is installing PostgreSQL.
How to go about this?
Any help is much appreciated!

IMPORTANT NOTE: This may not be the most ethical/proper way of doing this, but it appears to get the job done.
After numerous hours of digging, using the registry key on Windows is the best methodology, it seems, to determine if the version of PostgreSQL you intend to install, if it is already installed.
Assuming that I am running on the Windows system with Administrative rights, in theory, I should be able to change the login credential requirements of the pg_hba.conf file in the data directory (i.e. cluster) that already exists to allow myself to create the database(s) and user(s) I need to, before reverting the credential requirement settings to what they originally were.
So, the answer I have come to the conclusion with is:
Determine if PostgreSQL is already installed or not. Look at the HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installation\[version] registry, where [version] is formatted as postgresql-[32 or 64 bit]-[PostgreSQL version], e.g. postgresql-x64-12). If the registry exists, then data should exist for the Data Directory value... obtain that data, and that's where the cluster is located.
Step [2] and on are obviously for when PostgreSQL is already installed.
Make a copy of the pg_hba.conf file in the cluster directory provided by the Data Directory value from step [1].
This will be the file we restore to after we are done. Save file to a temporary directory, such as Desktop.
In the pg_hba.conf file in the cluster directory, change all connection types' methods to trust
Example:
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust
Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory>
NOTE: pg_ctl.exe is located under the \bin\ folder of Postgres' installation directory.
Example: C:\Program Files\PostgreSQL\12\bin\pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\12\data\"
Connect to the cluster and issue the command to create a superuser role for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE ROLE <role name> LOGIN SUPERUSER PASSWORD '<password>';
In the above command, I have the cluster running on the local computer (i.e. localhost, IP address 127.0.0.1) on port # 5432 (default), connecting to the default database postgres and issuing the command to create a role with whatever role name provided in place of <role name>, with SUPERUSER rights and the password provided in place of <password>.
Since one HAS to connect to a database, I am connecting to the default one postgres, otherwise template0 and template1 are default databases that could also be utilized.
Connect to the cluster and issue the command to create the needed database for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE DATABASE <database name>;
Replace the pg_hba.conf file with the original
Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory>

Related

FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)

I recently installed Postgresql 11, during the installation, there's no step to put password and username for Postgres. Now in pgAdmin 4, I wanted to connect the database to server and it's asking me to input password, and I haven't put any in the first place.
Any one knows what's going on. Thank you!
The default authentication mode for PostgreSQL is set to ident.
You can access your pgpass.conf via pgAdmin -> Files -> open pgpass.conf
That will give you the path of pgpass.conf at the bottom of the window (official documentation).
After knowing the location, you can open this file and edit it to your liking.
If that doesn't work, you can:
Find your pg_hba.conf, usually located under C:\Program Files\PostgreSQL\9.1\data\pg_hba.conf
If necessary, set the permissions on it so that you can modify it. Your user account might not be able to do so until you use the security tab in the properties dialog to give yourself that right by using an admin override.
Alternately, find notepad or notepad++ in your start menu, right click, choose "Run as administrator", then use File->Open to open pg_hba.conf that way.
Edit it to set the "host" line for user "postgres" on host "127.0.0.1/32" to "trust". You can add the line if it isn't there; just insert host all postgres 127.0.0.1/32 trust before any other lines. (You can ignore comments, lines beginning with #).
Restart the PostgreSQL service from the Services control panel (start->run->services.msc)
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
Remove the line you added to pg_hba.conf or change it back
Restart PostgreSQL again to bring the changes to effect.
Here is an example of the pg_hba.conf file (METHOD is already set to trust):
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
NOTE: Remember to change the METHOD back to md5 or other auth-methods listed here after changing your password (as stated above).
For Windows variant - I too experienced this nasty bug because of pgAdmin for my Windows x64 install of version 9.2. It left my production paralyzed.
In folder C:\Program Files\PostgreSQL\9.2\data or C:\Program Files (x86)\PostgreSQL\9.x\data, you'll find the pg_hba.conf text file.
Find the following lines:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
and change METHOD md5 to "trust" like this:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
From Windows>Run type "services.msc" and enter find the right PostgreSQL instance and restart it.
Your DB security is now blown wide open! Heed the warning to return it back to md5 after changing the user password expiry time to say year 2099 for all the relevant users.
Change the password of default use
ALTER USER postgres WITH PASSWORD 'new_password';
Note: CREATE USER is the same as CREATE ROLE except that it implies LOGIN.
$ psql postgres
postgres=# create user postgres with superuser password 'postgres';
After successfully changing the master password
If you get the same error even after following the master password reset steps
Open your command prompt and execute
psql -U postgres
It will ask you for the password, enter the new password which you set now parallelly open SQL shell(psql) and try again with the new password
I have tried all the above mentioned solutions, trust me northing worked! I have resolved the issue by using following commands
psql -U default
\password
Enter new password:
Enter it again:
my username is : default
This worked perfectly for me.
For Linux user try this
//CHECK POSTGRES IS WORKING OR NOT
sudo systemctl status postgresql
//THIS WILL ACCEPT PORTS
sudo pg_isready
sudo su postgres
//NAVIGATE TO SQL TERMINAL / BASH
psql
//CREATE A NEW USER WITH PASSWORD
CREATE USER shayon WITH PASSWORD 'shayon';
try using psql -U postgres if have put password while installing this is command where you have to use that. Thank you :)
Option 1: If you use trust
Better change only postgres to trust in the pg_hba.conf, then access your db with postgres super user and add other users and passwords with the power of the postgres super user, then change all other peer to md5.
The steps: In the pg_hba.conf, change
local postgres to trust
do not change local all to trust,
instead change local all from peer to md5 - which means that a right password is enough to login.
See this solution in detail at the second answer of 'Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails'.
Option 2: Use md5, no trust needed (recommended)
This way is even easier because you will need to change the pg_hba.conf only once:
Change any local user from peer to md5, usually:
Change local postgres from peer to md5
Change local all from peer to md5
Add a postgres pw with the power of your Linux pw only:
sudo su postgres
psql (or psql -p <port> if you have more than one PostgreSQL)
\password
\q
See the accepted answer and the comments of "Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails".
I solved this problem by changing peer to trust in the file "pg_hba.conf" at local postgres then I restarted the postgres service with the command:
sudo service postgresql restart
That's it.
This particular situation I'm about to mention probably doesn't come up very often, but I was getting this error as well. After looking into it, it was because I had a local postgres instance listening on port 5433, and I was trying to set up a Kubernetes tunnel to a remote PG instance mapped to local port 5433 as well. It turns out the command I was running was attempting to connect to the local instance rather than the remote instance. When I temporarily stopped the local instance, I was able to connect to the remote instance through the tunnel without changing the psql command I was using.
I know this is an old question, but I had the same problem, e.g. no dialog for setting password for Postgres during installation with Postgresql 11.
Instead of doing all the file manipulations suggested in the other answers, I deleted Postgresql 11 and installed Postgresql 12, where I was prompted for setting password during installation.
Loggin to PgAdmin4
Go to
Object > Create > Login/Group Role
Create the "username" that was named in the psql terminal
Create password
Give it all the rights
Save
try the password immediately in the psql terminal.
It worked for me.
Hope this works for you.
You can use the "superuser" password for the first time.
After that you can use Object > Create > Login/Group Role to change the password for the "postgres" user.
I currently had a headhache solving this case. A friend helped me I decided to post my solution here.
Open pg_hba.conf in any text editor (you can find this file in your postgres instalation folder > data);
Change all the methods fields to trust (meaning you don't need a password for postgre);
Run in your console this comand: "alter user postgres with password '[my password]';" | psql -U postgres (meaning to alter some user password for [my password] for the user as parameter -U postgres);
Et voilà (don't forget to change back the method from trust for the one that should be best for you).
I hope this help someone someday.
For those of you who got this error and NONE of these answers helped, I may not have StackOverflow fish for you, but I'll teach you how to fish!
You likely don't have the correct order of lines in the pg_hba.conf file. If you read this PostgreSQL documentation link below, it says this error can be thrown if "no matching entry is found". However, that is NOT always true! Documentation is written by humans and humans make mistakes.
https://www.postgresql.org/docs/current/client-authentication-problems.html
The truth is that a line further up might take precedence, is qualifying and is forcing you to use a password stored in PostgreSQL rather than delegated authentication or some other method. If you are not specifying a password stored in PostgreSQL, then you do not need the LOGIN role attribute. Put a line at the very top of this list with your specific user, authentication protocol, network details and other criteria. Also, many may think that most computers use IPv4. Try IPv6 and you'll be surprised. Once you know the very specific criteria of your issue and place a line at the top, then you have established the ONLY RELIABLE WAY to troubleshoot these pg_hba.conf issues without source code debugging!
Another helpful trick is to create a crapload of Server entries in pg_admin (SQL IDE for PostgreSQL) with all of your users and authentication protocols for testing. When you test different scenarios, you'll instantly know which ones fail.
Also, whenever you change this file, restart the PostgreSQL service, before testing the user.
You're welcome my friend. :)
Follow below stepsif you are using pgAdmin4 and facing error in updating password :
1] Open file "pg_hba.conf" and find "IPv4 local connections"
2] See the value under "Method" column, it must be set to "md5" becase you selected it while installing.
3] Make "md5" value blank and save the file. Restart pgAdmin4 application.
4] Now again set the value back to "md5" and input your password in pgAdmin application.
You should be successfully able to do it.
windown 11 - postgres 14
open pgAdmin4 - click servers
right-click on your windows user name rule, e.g: MyUserName.
definition tab - enter password, click save.
open/re-open terminal
run: psql "postgres:///"
if you get "MyName database doesn't exist" you're good to go

Querying hive schema with HAWQ and PXF

I know it's possible to query data managed by Hive with HAWQ, and HCatalog, I have installed Hortonworks ambari that installs all the tools needed. To query the hive i schema followed pivotal instructions where it says to enter PostgreSQL and do the command SET pxf_service_address TO "localhost:51200", but I always get the error unrecognized configuration parameter "pxf_service_address". Does anyone have this problem before?
Your version() output indicates you are connecting to a PostgreSQL instance and this is NOT HAWQ.
The default port for both PostgreSQL and HAWQ is 5432 so on a single node, one has to be changed. I think it is a bug that Hortonworks has decided to change the HAWQ port instead of the PostgreSQL port. So if you want to give some feedback to Hortonworks, tell them to change the PostreSQL port to something other than 5432.
Find hawq_master_address_port in your /usr/local/hawq/etc/hawq-site.xml file. The value will be something other than 5432. You should also look at the hawq_master_address_host value and make sure you are on the Master host. So ssh to your master host to connect via psql.
Now, armed with your HAWQ port and logged in as gpadmin on the Master host, edit your .bashrc file. Add:
export PGPORT=<hawq_master_address_port>
Substitute hawq_master_address_port with the value from hawq-site.xml.
Now source your .bashrc file and then run:
psql template1
I'm also guessing you don't have a gpadmin database so create it.
psql -d template1 -c "create database gpadmin;"
Now you can just run "psql" and connect to HAWQ.

postgres pg_basebackup can't find .pg_pass file

I have successfully setup replication streaming from a primary to a secondary postgres docker container, each running as tasks on separate ec2 instances.
However, I did this by leaving the replication user on the primary server as trust in its pg_hba.conf:
host replication replication 0.0.0.0/0 trust
Then, when I switched it to md5, I thought I would be able to simply set a password on the secondary for the replication user and everything would be fine. Nope.
In my initialization script on the secondary, when I call
pg_basebackup -h #{primary_ip} -p 5432 -D $PGDATA -U #{repl_user} -v -P -w --xlog-method=stream
I initially got the password prompt.
Then I added the -w. Which would give me the error:
pg_basebackup: could not connect to server: fe_sendauth: no password supplied
Then I found out there is no postgres home directory on the generic postgres 9.6 image, so I added a $PG_PASSFILE variable. That didn't work (Permissions were fine, I even put it in /tmp as well as passed the PG_PASSFILE=... right on the pg_basebackup line as in this question: .pgpass for PostgreSQL replication in Dockerized environment (see Raphael's comment))
No matter what I do, the pg_basebackup ignores the .pg_pass file.
I then tried mounting a volume as /home/postgres but with AWS, I can't seem to gosu to root inside the entry point init script I have created. Everything is as the postgres user.
Has anybody overcome this?
I am running my secondary initialization code as an entrypoint script. Like I said, it works fine as trust but adding that password is killing me.
If you set replication you dont need to run pg_basebackup any more. adding -w will always give you fe_sendauth: no password supplied if you set anything but trust or peer for local to corresponding connection - just because -w stands for --no-password.
https://www.postgresql.org/docs/current/static/libpq-envars.html
PGPASSFILE specifies the name of the password file to use for lookups.
If not set, it defaults to ~/.pgpass (see Section 32.15).
so default is .pgpass, not .pg_pass, of course you can use .pg_pass, setting export PGPASSFILE=.pg_pass , but you use PG_PASSFILE variable - right?..
So postgres ignores it, and it should.
I would first try creating right .pgpass in home directory with 600 permissions.
Also mind different primary_conninfo if you want to use password file.
https://www.postgresql.org/docs/current/static/standby-settings.html
in a separate ~/.pgpass file on the standby server (use replication as
the database name). Do not specify a database name in the
primary_conninfo string.

postgres server started but could not connect to server

After I lost all the data on my computer (had to have everything erased by the Apple store), I installed postgres on my Mac using Homebrew with the command brew install postgres. I then started the server (with confirmation that the server was started) and tried to create a database for a Sinatra project that uses postgres, using the command
createdb db_development
However, I got the following error:
createdb: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
After searching online, I discovered that I might have to run the following initdb command
initdb /usr/local/var/postgres
but when I did it, I got this message
initdb /usr/local/var/postgres
The files belonging to this database system will be owned by user "braindead".
This user must also own the server process.
The database cluster will be initialized with locale "en_CA.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
So, why might postgres have confirmed the server had started at the same time I got this error could not connect to database postgres: could not connect to server: No such file or directory, and how could I fix this?
the READ.me for the project said
bundle install
createdb db_development
rake db:migrate
It either tries to connect to the postgres that comes with MacOS or PG environment variables are not set.
Try:
createdb -h localhost db_development
these are postgres environment variables for postgres. Also PostgresApp is a very convenient way to use Postgresql on Mac
Postgres has a "unix_socket_directory" setting located in postgresql.conf
Please verify that this setting exists and that the directory itself exists, then restart your server. If it doesn't exist, set it to something like /var/run (or another directory that exists) and restart the server.

pgadmin gives me the error: no password supplied

I've installed postgresql 9.2 on linux (kubuntu) and the last version of pgadmin3, but when I connect them I have this error:
An error has occurred:
Error connecting to the server: fe_sendauth: no password supplied
What can I do?
I have also configured tomcat for my web application in java. In fact, postgresql was working before trying my application.
Change the password for role postgres:
sudo -u postgres psql postgres
alter user postgres with password 'postgres';
Try connect using "postgres" for both username and password.
Refer to: How to change PostgreSQL user password
Whether a password is required depends on your settings in pg_hba.conf. And there are different ways you can connect - different settings in pg_hba.conf may apply.
I quote the help shipped with pgAdmin 3 for the "Host" field in the connection ("server") settings:
The host is the IP address of the machine to contact, or the fully
qualified domain name. On Unix based systems, the address field may be
left blank to use the default PostgreSQL Unix Domain Socket on the
local machine, or be set to an alternate path containing a PostgreSQL
socket. If a path is entered, it must begin with a “/”. The port
number may also be specified.
If you connect via Unix socket the rules for "local" apply.
Whereas when connecting via TCP/IP "host" (or "hostssl") rules applies.
If you have a line like this at the top your pg_hba.conf file:
local all all peer
or:
local all all ident
.. then you can connect locally without password if your system user is "postgres" and your database user is "postgres", too.
I realize this is question is years old, but I ran into this same problem today and have a solution that uses trust in a limited but useful way.
As in many development shops, when the devs need a QA postgres password, they just yell it, message it, email it, write it on their foreheads, etc. And I'm like, "This is really bad. I need to figure out a way to use PKI here." We also use pgAdmin3.
First, add a line like this to your pg_hba.conf, where dev represents the user for the developers in your shop:
host all dev 127.0.0.1/32 trust
Drop the developers' public key in their authorized_keys folder on the database server. Now have them ssh into the server with the -L flag with a command similar to the following:
ssh -i ~/.ssh/id_rsa -L5432:127.0.0.1:5432 -vvv 101.102.103.104
This allows one to use the postgres port as if it were localhost. Of course, replace the key, server and make sure to map to an open port locally (if you have a local postgres running, it's probably bound to 5432). I use a pretty verbose flag so I can easily troubleshoot any ssh issues.
Open another terminal and issue this command:
psql -h 127.0.0.1 -U dev -p 5432
You should have access to the database and never be prompted for a password, which I think is great because otherwise, the devs will just waive the password around with little regard to security, passing it out like Halloween candy.
As of now, PgAdmin3 will still prompt you for a password, even though -- plain as day -- you do not need it. But other postgres GUIs will not. Try Postico. It's in beta but works great.
I hope this answer helps anyone like me who would rather use PKI for postgres auth rather than sharing passwords willy-nilly.
Met this problem recently.
If you're using PostgreSQL on local machine, and psql works well without logging needed, try pgadmin3's menu File - Add Server - Properties tab, fill in Name field for this connection, leave Host field and Password field empty, and click ok.
from pgadmin docs
On Unix based systems, the address field may be left blank to use the
default PostgreSQL Unix Domain Socket on the local machine, or be set
to an alternate path containing a PostgreSQL socket. If a path is
entered, it must begin with a “/”.
Worked on Debian testing (pgadmin3 1.22, PostgreSQL 11), without touching pg_hba.conf.
For me, I run pg_ctl -D /usr/local/var/postgres start, start the server, then everything is OK, it will pop out the connection host port.