Changing postgres password in version 9.4 - postgresql

I have PostgreSql 9.3 version installed in my ubuntu 14.04 machine. I just installed the 9.4 version as well and the port it is on is 5433(by default). When i give the command psql --version, it gives me the following:
psql (PostgreSQL) 9.4.1
So far so good! Now i am trying to change my password for my postgresql 9.4 and i gave the following command:
>>sudo -u postgres -p 5433 psql
psql (9.4.1, server 9.3.6)
Type "help" for help.
postgres=# alter user postgres with password 'password';
ALTER ROLE
postgres=# \q
Now i tried connecting it on pgadmin3 but it would give me the password authentication failure. Am i doing it right?

sudo -u postgres psql -p 5433
Your -p 5433 as you write it is treated as an option for sudo, not for psql. And you've connected to Postgres 9.3 on standard port.

Try using:
>>sudo -u postgres -p 5433 psql
psql (9.4.1, server 9.3.6)
Type "help" for help.
postgres=# \password
Enter new password:
Enter it again:
postgres=#

Related

How can connect psql with normal user without specifying database name?

I can connect mysql with username without specifying any database name,showkey is one of mysql user:
mysql -u showkey -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.31-MariaDB-0+deb10u1 Debian 10
Now i log into mysql without specifying any database name.showkey is also a normal user for my postgresql,test is a database name in my postgresql.
psql -d test -U showkey
Timing is on.
Pager usage is off.
Null display is "missing data".
psql (11.14 (Debian 11.14-0+deb10u1))
Type "help" for help.
test=#
I conncet the test database with a normal user showkey,now want to connect postgresql with normal user without specifying database name such way as in mysql:
psql -U showkey
psql: FATAL: database "showkey" does not exist
sudo -u postgres psql can connect postgresql with a super user instead of with normal user.

Dual Postgres Instances config_file issue

I have a sevrer that has an instance of postgres 9.4 I am using.
I installed another version of postgres (9.6), but have come across an issue.
To init the new db, I ran
sudo -u postgres /usr/pgsql-9.6/bin/initdb -D /var/lib/pgsql/9.6/data/
But when I check the sql, I get the following.
sudo -u postgres /usr/pgsql-9.6/bin/psql
psql (9.6.9, server 9.4.18)
Type "help" for help.
postgres=# SHOW config_file;
config_file
-----------------------------------------
/var/lib/pgsql/9.4/data/postgresql.conf
I can see it is using my 9.4, but I am unsure how to correct this issue, and have not managed to find any resources that indicate how to do so.
Any help would be appreciated.
The 9.4 postgres use the default port and I guess you set another one for postgres 9.6.
Connect to it with psql -P xxxx where xxxx is the 9.6 port number.

Cannot connect to Postgres DB via psql cmd

When I issued the command:
psql -h localhost -U ruanpc
The console fails to connect to localhost postgres db and display:
psql: symbol lookup error: psql: undefined symbol: PQsetErrorContextVisibility
My postgres and psql versions are:
ruanpc#slave-40:~$ postgres --version
postgres (PostgreSQL) 11devel
ruanpc#slave-40:~$ psql --version
psql (PostgreSQL) 11devel
Any one knows how to solve it?
Your psql loads a libpq with version 9.5 or older. Make sure to use the same version of libpq as the psql version.
Oh, I forget to set the LD_LIBRARY_PATH to point to Postgres shared library!
Easy fix for me!

What does psql -d postgres do?

What is the -d for and why do you need to type postgres when the psql already stands for postgres?
psql -d postgres
psql -d postgres
here -d stands for database name and hence postgres is your database name.
You can open the terminal by only typing psql, Above statement will do two things for you
Open psql terminal
Connect you to postgres db
For more psql options you can refer https://www.postgresql.org/docs/current/static/app-psql.html

Create database from command line

I am trying to create a database from command line.
My OS is centos and postgres version is 10.9.
sudo -u postgres psql createdb test
Password for user test:
Why is it prompting me for the password?
Change the user to postgres :
su - postgres
Create User for Postgres (in the shell and NOT with psql)
$ createuser testuser
Create Database (same)
$ createdb testdb
Acces the postgres Shell
psql ( enter the password for postgressql)
Provide the privileges to the postgres user
$ alter user testuser with encrypted password 'qwerty';
$ grant all privileges on database testdb to testuser;
Try:
sudo -u postgres psql -c 'create database test;'
createdb is a command line utility which you can run from bash and not from psql. To create a database from psql, use the create database statement like so:
create database [databasename];
Note: be sure to always end your SQL statements with ;
As some of the answers point out, createdb is a command line utility that could be used to create database.
Assuming you have a user named dbuser, the following command could be used to create a database and provide access to dbuser:
createdb -h localhost -p 5432 -U dbuser testdb
Replace localhost with your correct DB host name, 5432 with correct DB port, and testdb with the database name you want to create.
Now psql could be used to connect to this newly created database:
psql -h localhost -p 5432 -U dbuser -d testdb
Tested with createdb and psql versions 9.4.15.
As the default configuration of Postgres, a user called postgres is made and the user postgres has full super admin access to entire PostgreSQL instance running on your OS.
sudo -u postgres psql
The above command gets you the psql command line interface in admin mode.
Creating user
sudo -u postgres createuser <username>
Creating Database
sudo -u postgres createdb <dbname>
NOTE: < > are not to be used while writing command, they are used just to signify the variables
PostgreSQL Create Database - Steps to create database in Postgres.
Login to server using postgres user.su - postgres
Connect to postgresql database.
bash-4.1$ psql
psql (12.1)
Type "help" for help.
postgres=#
Execute below command to create database.
CREATE DATABASE database_name;
Check for detailed information below:
https://orahow.com/postgresql-create-database/
PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB
With a single command line:
su -c "createuser dbuser;createdb -h localhost -p 5432 -E UTF8 -O dbuser dbname;" - postgres
It is pretty simple but sometimes I find the answers tricky.
[For windows users]
Open Windows cmd
psql -U <username>
Once connected to psql, enter the following command to create a new database: CREATE DATABASE <database_name>;
To verify that the database has been created, you can run the \l command to list all available databases. Your new database should be listed in the output.------[Additional]------
You can now connect to the new database using the \c command followed by the database name, like this: \c <database_name>
You can now run SQL commands on the new database to create tables, insert data, and so on.
Note: Make sure to replace <username>, <database_name> with your actual Postgres username and database name.
If you are using pgAdmin:
In query editor you can try like this :
CREATE DATABASE <databasename>
WITH
OWNER = <dbowner>
ENCODING = <encoding>
CONNECTION LIMIT = <numberofsimulaneousconnections>;
an example snippet :
CREATE DATABASE twitterdb
WITH
OWNER = postgres
ENCODING = 'UTF8'
CONNECTION LIMIT = -1;