What do I need to specify to get .pgpass to work? - postgresql

Can someone point me to a piece of documentation that specifies the matching rules psql applies to the .pgpass file? I always spend a few extra keystrokes trying to find the right combination of host, port, database, username to get it to connect.
Some things are obvious (i.e. if my linux username is not the same as my postgres username than I need to specify it) but based on what I've seen there is some non-obvious behavior.

Here is the documentation you are probably looking for. It explains the rules pretty well.
Just so this isn't a link-only answer, here are the rules:
Each .pgpass entry is on its own line and has the following format:
hostname:port:database:username:password
You can use a wildcard (*) for each field except password
If you use wildcards, put more specific entries first
You can use for instance localhost:*:*:foo:password to specify password for user foo on your local machine regardless of the database you are connecting to or the port the Postgres is actually running on.

Related

How do PostgreSQL users with same name as operating system users work?

I stumbled upon PostgreSQL installations, where there is a PostgreSQL user with the same name as an operating system (Linux) user. If you are logged into the operating system as such a user, you can usually login to psql without specifying a username and password as this user.
How does this work? Is this a pure convention, or is there a special mechanism in PostgreSQL for this kind of behavior?
There are cases, where no corresponding PostgreSQL user is present for an operating system user. In this case, how do I create one, that can be logged into without having to specify username and password once I am logged into the operating system as this user?
There are several mechanisms. I don't know what makes a mechanism a special one, though.
The client always sends a username to the server. If you don't specify one to use then libpq-based clients look up the operating system name and use that. Some non-libpq-based clients might adopt the same convention.
For not requiring a password, maybe local connections are set to 'trust' in pg_hba.conf, in which case anyone else on the same machine can also log in to PostgreSQL as you, just by specifying your username to use, such as with -U martinw. Or maybe a password is required, but it is provided automatically via .pgpass file or via PGPASSWORD env variable, but if you set that up presumably you would remember doing so. Or maybe you have local connections set to peer in pg_hba, in which case the db server asks the OS "who is on the other end of this socket?" and verifies the answer matches the requested username (this is a common default setup, for example on Ubuntu Linux) or if a pg_ident map is in use, then it verifies the response system username is allowed to log in as the specified db username.
https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-PASSFILE
create a pgpass file: vim ~/.pgpass
fill your credentials: hostname:port:database:username:password
exit file. and change ~/.pgpassfile access permission:
https://www.postgresql.org/docs/current/libpq-pgpass.html
chmod 0600 ~/.pgpass
If your linux user is the same as one of the pgpassfile user, then just type psql to connect to database.

How to setup a password for PostgreSQL in postgreapp?

I am using postgresapp for the PostgreSQL and without password I was able to connect to the database and perform operations. I am curious to learn about the password. Also I use Postico as Interface. Open to any suggestions.
The default for Postgres.app is to have no password and set trust-level authentication in pg_hba.conf. To change this, you need to do the following:
Alter the IP address and mask for host all all 127.0.0.1/32 trust as needed in pg_hba.conf, and change authentication method from trust to password or md5 (or whatever your requirements are)
Set the password for the desired user(s) with ALTER USER <username> WITH PASSWORD '<password>';
Reload the conf with SELECT pg_reload_conf()
Note your pg_hba.conf file is usually located in ~/Library/Application Support/Postgres/var-12 -- the sure-fire way to know is by querying SHOW data_directory in your psql prompt
Postgres.app is a great way to get Postgres running on macOS in a few minutes. It ships with a default user name and password, and they you're on your own. Postgres.app is a nicely compiled version of Postgres that you can run by double-clicking, you'll need other tools (and knowledge) to take advantage of Postgres. As you'll have noticed, the UI for Postgres.app is pretty much a few buttons to configure a server, and to give you shortcuts to the logs, configuration files, and data.
If you want to use psql (as mentioned), or any of the other command line tools, they're embedded in the application's package. Right-click, open the package, open Contents, open Versions, open the version you use, and look in bin.
If you want a GUI tool, there are many options. Since you say Postgres.app, I'll assume macOS. You've found Postico, SQLPro for Postgres is good, TablePlus is also good. Those tools have fairly uncluttered UIs. If you want or need more features, pgAdmin has a whole lot to offer, and it's free. I end up using Navicat a lot, even though it has a UI that screams "Look Ma! I wrote it in Java!" It gets a lot done. I'd say that day-to-day on macOS, I use SQLPro most. But, really, it's largely a matter of taste. psql is quite powerful, and you'll find no short of help for that.

.pgpass file does not work as advertised

(Debian 8)
My .pgpass file is at my home folder (admin)
I and using the right format as indicated in the documentation
hostname:port:database:username:password
The same fields I put there work well when put explicitly into the psql command line. (of course I have to enter the password manually).
However, running psql by itself gives our an error:
psql: FATAL: role "admin" does not exist
Note that my sql username is NOT admin, which is my debian username.
What am I doing wrong? my goal is to get access to psql without having to use an elaborate command line including host/port/username/database
.pgpass is not a way to choose which settings you want to use, it's a way to store passwords for a number of settings you've already chosen to use. It can contain multiple lines. The relevant line is then chosen as follows, according to the documentation:
The password field from the first line that matches the current connection parameters will be used.
You still have to provide your connection parameters (besides the password).
If you always want to use the same connection parameters, you should probably use the environment variables (PGHOST, PGDATABASE, PGUSER, ...), and possibly place them in your .bashrc file (depending on the shell you use).
You can then choose to store the password itself in the PGPASSWORD environment variable or in the .pgpass file. The latter might give you a bit more flexibility.

dokku + postgres : "the scheme postgres does not accept registry part"

I'm trying to run dokku on DigitalOcean to get a ruby/rails project up with postgres.
I got help to finding the logs, but I'm at loss when I see this :
/app/vendor/ruby-2.0.0/lib/ruby/2.0.0/uri/generic.rb:214:in `initialize': the scheme postgres does not accept registry part: root:aLZgAlQKbHbKhHHn#: (or bad hostname?) (URI::InvalidURIError)
any ideas on what I could perhaps look into?
the domain name is fritida.se the databasename i fritida (or was it fritida.se?)
I'm going to go and look for a way to list databases.
if your password contains unsafe characters
Connection string used for DATABASE_URL cannot contain special characters (anything other than [a-zA-Z0-9_~-\.], e.g. # is a common one to mess up). Solution is to url-encode.
see also https://stackoverflow.com/a/34280541/1733117

What is a straightforward way to connect to Postgres.app with dbext (in Vim)?

When I try to connect to my Postgres.app db using dbext, I get the following error:
dbext:PostgreSQL requires a '$HOME/.pgpass' file in order to authenticate. This file is
missing. The binary 'psql' does not accept commandline passwords.
Other programs connect just fine by using a "local" connection. (Postgres.app runs with my userid.)
In vim :!which psql correctly prints /opt/local/bin/psql (which I have symlinked to the one in the Postgres.app bin directory). And Postgres.app is set up to use "local" authentication and there's no clear sense of where a pg_hba.conf file would go (there is no etc directory in the app bundle). Moreover, Postgres.app doesn't have anything in its documentation about changing access configuration.
I've tried using dbext's :DBPromptForBufferParameters directly, as well as #tpope's vim-rails plugin (which returns without comment from dbext setup via :Rdbext.
So what do I do to get dbext to connect using a "local" connection?
Note - I spent a LOT of time trying to figure this out without trying the obvious, thus the post even when I already have the answer. I'm also curious to see if anyone else has a different approach.
It turns out you can just make an empty ~/.pgpass file (restricting read-write permissions to your userid only to avoid warnings). This was counterintuitive for me (since there is in fact no password), but I suppose in retrospect it's obvious I should have tried it.
I'll point this out on the dbext issue tracker.