Postgres quick start for SQL Server / T-SQL Developers

After 17 years on T-SQL, I at last started working on projects using Postgres. Here are the first things I needed for the transition.

  • Do this first: https://wiki.postgresql.org/wiki/First_steps as it answers first questions about connections, databases and login.
  • Postgres replaces logins and database users and groups with a single concept: ROLE.
    • Permit a ROLE to login with the With Login clause when you Create or Alter it:
      Create Role MyNameHere With Login.
    • Grant it access to a database with Grant Connect: Grant Connect on Database dbname TO roleName.
    • Make it a group with – well, it already is a group. Add other roles to it with Grant Role1 to Role2. I think this approach works out well both for evolving and managing users & groups.
  • No NVarchar is needed, just text. The default installation uses UTF-8. Text is the usual idiom over VarChar(n), allegedly it performs marginally better.
  • 'String concatenation is done with ' || ' a double pipe '.
  • Date & Time: use timestamp for datetime/datetime2. Read Date/Time Types for more on dates, times and intervals.
  • Use semicolon terminators nearly everywhere:
    If ... Then ... ; End If ; T-SQL doesn't need them but Postgres demands them.
  • Before version 11, use Function not Procedure (use returns void if there is no return value). From version 11, you can Create Procedure() but note brackets are needed even for no parameters. A Procedure is called with CALL.
  • A typical definition:
    Create or Replace Function procedureName( forId int, newName varchar(20) )
    Returns int -- use returns void for no return
    As $$
    Declare
      localvariable int;
      othervariable varchar(10);
    Begin
     Insert into mytable (id,name) values (forId, newName) On Conflict (id) Do Update Set Name=NewName ;
    End $$;
    
  • Ooh, did you notice the 'Create or Replace' syntax, and how Postgres has a really good Upsert syntax – Insert On Conflict Update ?
  • Postgres does function overloads, so to drop a function you must give the signature: Drop function functionname(int)
  • Function and Procedural code is strictly separated from “pure” SQL in a way that T-SQL just doesn't bother with, and is typically written in plpgsql script. Declare variables in one block before Begin. For interactive work you can use an Anonymous Function block starting with DO :
    Select 'This line is plain SQL' ;
    Do $$
    Declare
      anumber integer ;
      astring varchar(100);
    Begin
    If 1=1 Then
     Raise Notice 'This block is plpgsql' ;
    End If;
    End $$ ;
    Select 'This line is SQL again' ;
    

    In fact, Postgres functions are defined in a string. The $$ delimiter is not a special syntax for functions, it is the postgres syntax for a literal string constant in which you don't have to escape any special characters at all. So that's handy for multiline strings and quotes.

  • Functions can be defined in other languages than plpgsql. For javascript, for example, see https://github.com/plv8/plv8.
  • If you don't need variables other than parameters, or control statements you can write a routine in pure sql by specifying:
    Create or Replace Procedure ProcName(Date onDate)
    Language Sql
    As $$
    Select * from LatestNews where PublishTimeStamp::Date = onDate
    $$;
    
  • The double colon :: is the cast/conversion operator.
  • Whereas the T-SQLer does everything in T-SQL, other database systems use non-sql commands for common tasks. For working at a command-line, learn about psql meta-commands. Start with \c to change database, \l to list databases, \d to list relations (ie tables), and \? to list other meta commands.
  • Variables: are only available inside plpgsql (or other language) code, not in plain SQL. Just use a plain identifier myvariablename with no @ symbol or other decoration. BUT as a consequence you must avoid variable names in a query that are the same as a column name in the same query. BUT BUT in Ado.Net Commands with Parameters, still use the @parametername syntax as you would for SQL Server
  • Postgres has probably stayed ahead of of T-SQL in keeping close to the SQL standard syntax, though they both become more, not less, standards-compliant with each version. The docs go into detail on deviations. But this means that much of your code for databases, schemas, users, tables, views, etc can be translated fairly quickly.
  • Replace Identity with the SQL Standard GENERATED BY DEFAULT AS IDENTITY for Postgres 10 onwards. (You will see Serial in older versions). For more complex options, Postgres uses SQL Sequences.
  • The Nuget package for Ado.Net, including for .Net core, is Npgsql
  • Scan these

Converting T-SQL to Postgres

Here's my initial search/replace list for converting existing code:

Search Replace
@variableName _VariableName
but stay with @variable for .Net clients using Npgsql
NVarChar Text
datetime
datetime2
timestamp
Identity Generated By Default As Identity
Before postgres 10 use Serial
Raise Raise Exception
Print Raise Notice
Select Top 10 ... Select ... Fetch First 10 Rows Only ;
Insert Into table Select ... Insert into Table (COLNAMES) Select ...
If Then Begin … End
Else Begin … End
If Then … ;
Else … ; End If ;
Create Table #Name Create Temporary Table _Name
IsNull Coalesce
UniqueIdentifier uuid
NewId() Before version 13, first websearch for Create Extension "uuid-ossp" to install the guid extension to a database. Then you can use uuid_generate_v4() and other uuid functions
Alter Function|Procedure Create or Replace Function

Authorization

Usually you will connect to postgres specifying a username. The psql.exe commandline tool will default it to your OS login username. You can avoid passwords in scripts that use psql by putting them in the pgpass.conf file).

If you want to set up integrated security, the limitation is that for computers not in a Domain it only works on localhost. The instructions at Postgres using Integrated Security on Windows take about 5 minutes for the localhost case, and include a link to the extra steps for the Domain case.

GUI Admin

Your replacement for SQL Server Manager is the pgAdmin GUI which gets you nicely off the ground with a live monitoring dashboard.

Command Line and Scripting

psql -h localhost -d myDB -c 'Select Current_User'

runs the quoted command. Note -h for “host”, not -s for “server”. Use psql --help to see more options. You can also use the pipe:

echo 'Select Version(), Current_Database();
Select Current_TimeStamp' | psql -h localhost

For large data dumps, pg_dump and pg_restore are designed to generate consistent backups of entire databases or selected tables —data or DDL or both—without blocking other users.

There are more command line utilities

Lost SQL Server sa password ? How to start up and login in single user mode

The problem: Someone has lost the sa admin password for your MS Sql Server; or the one person who has SQL admin rights has left the company. Alas, you find that even having Windows admin rights does not grant you access because you have a recent version of Sql Server and you didn't grant Sql Server admin rights to the machine or domain admins.

You can still fix this. You will need local admin right on the machine, and the ability to:

  • open a command line as an administrator
  • look through the registry with RegEdit to find the settings for the version and instance of Sql Server you are locked out of. MSDN mssqlserverloginmode-registry-key has some clues.
  • look through Program Files\Microsoft SQL Server\ and find the binn directory for your version and instance of Sql Server.

The trick is to start Sql Server in single user mode, and then login as a local admin. This will give you admin access to the SQl Server.

How to Get Admin Access to Sql Server on Your Machine

  1. Stop the sql service.
    • This is most easily done via the Windows Services Gui, but net stop MSSQLSERVER might do it. If you have a named instance use net stop MSSQL$instancename
  2. Work out the file location and registry key for the version/instance name of sql server you are trying to get into. This may be trickier than you think - you may have SqlExpress as well as more than one version and instance name of MSSQLServer. For instance:
    • C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn and
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQLServer\

    or

    • C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn and
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.SQLEXPRESS
  3. Change the registry entry for loginmode to 2 (not 0 or 1), which enables both Windows and SQL authentication.
  4. Open a command line window as administrator and navigate to the binn directory you found earlier under C:\Program Files\Microsoft SQL Server\. Run sql from the command line using the –f parameter, sqlservr.exe -f
    • You may need more command line parameters to get your instance running properly, although I never have so far. If so, use the Windows Services Gui to see what the rest of your command line has to be.
    • For a named instance, your command line is sqlserver.exe -f -s instancename
    • An alternative to -f is -m, but -f worked for me.
  5. Open another commandline, also as administrator, and run sqlcmd –S <servername>. Sqlcmd is usually on the path, but if not it should be in the same directory as sqlservr.exe.
    • The server name for local machine is of course '.', as in sqlcmd -S .
  6. Now you can type T-SQL commands. Try Select @@ServerName, @@Version just for fun.
  7. Note that after typing your commands you must type GO and enter before anything you've typed gets sent to the server.
  8. Add yourself to the sysadmin role:
    EXEC sp_addsrvrolemember 'DomainName\LoginName', 'sysadmin'
  9. Or, enable the sa login and set the password with 2 lines of T-Sql:
    Alter login sa With Password= '<enterStrongPasswordHere>'
    Alter login sa Enable
    Go
  10. Exit and close both command windows.
  11. Restart the Sql Server service from the services Gui or with net start MSSQLSERVER or net start MSSQL$instancename

Done.

sp_WhoLock – a T-SQL stored proc combining sp_who and sp_lock to show who’s locking what and how much

Run this query, or remove the comments to actually create a stored proc.
Note that SQL pre-2008 requires a field to be commented out of the query, as noted inline.

/*
--Create Procedure WhoLock
--AS

if object_id('tempdb..#locksummary') is not null Drop table #locksummary
if object_id('tempdb..#lock') is not null Drop table #lock
create table #lock (    spid int,    dbid int,    objId int,    indId int,    Type char(4),    resource nchar(32),    Mode char(8),    status char(6))
Insert into #lock exec sp_lock
if object_id('tempdb..#who') is not null Drop table #who
create table #who (     spid int, ecid int, status char(30),
            loginame char(128), hostname char(128),
            blk char(5), dbname char(128), cmd char(16)
            --
            , request_id INT --Remove this line for SQL 2005 or earlier
            --
         )
Insert into #who exec sp_who
Print '-----------------------------------------'
Print 'Lock Summary for ' + @@servername  + ' (excluding tempdb):'
Print '-----------------------------------------' + Char(10)
Select     left(loginame, 28) as loginame, 
    left(db_name(dbid),128) as DB,
    left(object_name(objID),30) as object,
    max(mode) as [ToLevel],
    Count(*) as [How Many],
    Max(Case When mode= 'X' Then cmd Else null End) as [Xclusive lock for command],
    l.spid, hostname
into #LockSummary
from #lock l join #who w on l.spid= w.spid
where dbID != db_id('tempdb') and l.status='GRANT'
group by dbID, objID, l.spid, hostname, loginame

Select * from #LockSummary order by [ToLevel] Desc, [How Many] Desc, loginame, DB, object

Print '--------'
Print 'Who is blocking:'
Print '--------' + char(10)
SELECT p.spid
,convert(char(12), d.name) db_name
, program_name
, p.loginame
, convert(char(12), hostname) hostname
, cmd
, p.status
, p.blocked
, login_time
, last_batch
, p.spid
FROM      master..sysprocesses p
JOIN      master..sysdatabases d ON p.dbid =  d.dbid
WHERE     EXISTS (  SELECT 1
          FROM      master..sysprocesses p2
          WHERE     p2.blocked = p.spid )

Print '--------'
Print 'Details:'
Print '--------' + char(10)
Select     left(loginame, 30) as loginame,  l.spid,
    left(db_name(dbid),15) as DB,
    left(object_name(objID),40) as object,
    mode ,
    blk,
    l.status
from #lock l join #who w on l.spid= w.spid
where dbID != db_id('tempdb') and blk <>0
Order by mode desc, blk, loginame, dbID, objID, l.status

Although this is a bare metal approach to investigating sql contention, the query does show you actual login names, so it enables you to join up technical issues with affected/affecting customers.